我想我的问题很简单。
我想在触摸它时立即在画布上画一个点。点将被绘制到我触摸的位置。接下来我想在屏幕上绘制另一个点,但不是在第一个点的位置。意味着我想防止在另一个图像上绘制图像。
我试图用以下代码归档:
活动课
public class Draw extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawView = new DrawView(this);
setContentView(drawView);
drawView.requestFocus();
}
}
帆布类
public class DrawView extends View implements OnTouchListener {
private static final String TAG = "DrawView";
List<Point> points = new ArrayList<Point>();
Vector<Bitmap> bitmaps = new Vector<Bitmap>();
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
for (Point point : points) {
canvas.drawBitmap(bitmaps.get(point.i), point.x, point.y, paint);
}
}
public boolean onTouch(View view, MotionEvent event) {
Log.d("Touch","Touch");
Log.d("Points", "" + points.size());
Log.d("Bitmaps", "" + bitmaps.size());
int i = 0;
for (Point point : points) {
int height = bitmaps.get(i).getHeight();
int width = bitmaps.get(i).getWidth();
//Log.d("dimensions", height + ", " + width);
for (int x = 0; x <= width; x++) {
if (Math.floor(point.x + x) == Math.floor(event.getX())) {
points.get(i).isTaken = true;
Log.d("isTakenX", "" + point.isTaken);
break;
}
for (int y = 0; y <= height; y++) {
if (Math.floor(point.y + y) == Math.floor(event.getY())) {
points.get(i).isTaken = true;
Log.d("isTakenY", "" + point.isTaken);
break;
}
}
}
i++;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("EventDown","DOWN");
boolean isTaken = false;
for (Point point : points) {
if (point.isTaken) {
isTaken = true;
}
}
if (!isTaken) {
bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.greenstar));
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
point.i = bitmaps.size() - 1;
points.add(point);
//Log.d("pointClick",point.toString());
invalidate();
return true;
}
}
return false ;
}
}
class Point {
float x, y;
int i;
boolean isTaken = false;
@Override
public String toString() {
return i + ": " + x + ", " + y;
}
}
在 onTouch 方法中,您可以看到我如何确定我不想过度绘制的图像的位置。我将所有图像及其绘制的点保存到列表中,以便能够修改它们。接下来,我检查图像在屏幕上占用了多少空间,以及我是否只是单击了该空间。
但是,在将第一张图像绘制到画布上后,我无法再绘制另一张图像。似乎程序卡在“isTaken”上。可能我关于如何检查空间是否被占用的对数是错误的。但是,无法弄清楚出了什么问题。
你有什么想法吗?
提前致谢