大家好, 我有这张图片,我想在两点之间画线“路径”。
我尝试了什么:
是绘制路径,使用保存在数据库中的要点。 “x,y为点”,在多点之间画线,直到到达终点。像图片 2
我在想的是,如果我将图像划分为正方形网格,例如 15*15 ,并给每个正方形 x、y、标签,其中标签可以是 0,1,这意味着墙或任何东西 0 不能走路扔它 1 你可以。 并动态创建浴缸。
这就是我现在的所有想法 :) 如果还有其他方法我不知道。请帮助我:)
更新:
-用户可以放大/缩小。 -我想做的是类似this的事情
最佳答案
您可以直接修改位图,也可以在位图上放置一个 SurfaceView
并在 SurfaceView
上绘制。在这种情况下,您所要做的就是在 SurfaceView
的 onDraw
方法中绘制线条。
我在 Turtle Draw 中画了很多线应用...这是我如何扩展 SurfaceView
public class DrawView extends SurfaceView {
Paint paint = new Paint();
List < float[] > lines = new ArrayList < float[] > ();
List < Integer > colors = new ArrayList < Integer > ();
int curColor = Color.WHITE;
int bgColor = Color.BLACK;
Bitmap mBitmap;
ImageView turtle;
float curX, curY, curTurn = 0f;
Matrix transform = new Matrix();
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
mBitmap = getBitmapFromDrawable(context);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(DpiUtils.getPxFromDpi(getContext(), 2));
setScrollContainer(true);
// clear();
}
public DrawView(Context context) {
super(context);
}
public void addLine(float...l) {
synchronized(lines) {
lines.add(l);
colors.add(curColor);
}
}
public List < float[] > getLines() {
return lines;
}
public List < Integer > getColors() {
return colors;
}
@Override
public void onDraw(final Canvas canvas) {
synchronized(lines) {
super.onDraw(canvas);
int i = 0;
for (float[] l: lines) {
paint.setAntiAlias(true);
paint.setColor(colors.get(i++));
canvas.drawLines(l, paint);
curX = l[2];
curY = l[3];
}
transform.setTranslate(curX - 13, curY - 13);
transform.preRotate(360 - curTurn, 13, 13);
paint.setColor(Color.BLACK);
canvas.drawBitmap(mBitmap, transform, paint);
}
}
public void setTurn(float turn) {
this.curTurn = turn;
}
public void clear() {
lines.clear();
colors.clear();
DisplayMetrics metrics = DpiUtils.getDisplayMetrics(getContext());
curX = metrics.widthPixels / 2f;
curY = (metrics.heightPixels / 2f) - DpiUtils.getPxFromDpi(getContext(), 50);
curTurn = 0;
scrollTo(0, 0);
}
public static Bitmap getBitmapFromAsset(Context context, String strName) {
AssetManager assetManager = context.getAssets();
InputStream istr;
Bitmap bitmap = null;
try {
istr = assetManager.open(strName);
bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
return null;
}
return bitmap;
}
public static Bitmap getBitmapFromDrawable(Context context) {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.turtle_26);
return icon;
}
public void setDrawColor(int color) {
paint.setColor(color);
curColor = color;
}
public int getDrawColor() {
return curColor;
}
int x, y = 0;
int scrollByX, scrollByY = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = (event.getAction() & MotionEvent.ACTION_MASK);
if (action == MotionEvent.ACTION_DOWN) {
x = (int) event.getX() + scrollByX;
y = (int) event.getY() + scrollByY;
} else if (action == MotionEvent.ACTION_MOVE) {
scrollByX = x - (int) event.getX();
scrollByY = y - (int) event.getY();
scrollTo(scrollByX, scrollByY);
}
return true;
}
@Override
public void scrollTo(int x, int y) {
// TODO Auto-generated method stub
super.scrollTo(x, y);
scrollByX = x;
scrollByY = y;
}
@Override
public void setBackgroundColor(int color) {
// TODO Auto-generated method stub
super.setBackgroundColor(color);
bgColor = color;
}
public int getBackgroundColor() {
return bgColor;
}
}
关于android - 如何在android中的图像上的两点之间画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19040534/