期望的结果
我想在触摸屏幕的位置画一个红色圆圈,然后在触摸释放时将其删除。
当前结果
每当我触摸屏幕时,触摸位置会出现红色圆圈,但是当我松开触摸时,圆圈会停留在屏幕上。
额外帮助
我对Android的编码很新,如果快速浏览我的代码表明我正在使用任何不必要的东西,我很乐意被告知,所以我可以改进。
我的代码
package com.mr.mwood.thumbinput;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
class SampleView extends SurfaceView {
private final SurfaceHolder surfaceHolder;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
surfaceHolder = getHolder();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Canvas canvas = surfaceHolder.lockCanvas();
switch (event.getActionMasked()) {
case (MotionEvent.ACTION_DOWN):
canvas.drawColor(Color.BLACK);
canvas.drawCircle(event.getX(), event.getY(), 50, paint);
break;
case (MotionEvent.ACTION_UP):
canvas.drawColor(Color.BLACK);
break;
}
surfaceHolder.unlockCanvasAndPost(canvas);
return false;
}
}
}