我正在创建一个应用程序,我想在屏幕上绘制触摸事件的形状。任何ApI都能特别获得触摸事件的形状?
非常感谢任何示例或代码段。
由于 纳拉
答案 0 :(得分:1)
此代码段应达到您的目的:
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawingView(this));
}
class DrawingView extends SurfaceView {
private final SurfaceHolder surfaceHolder;
private final Path path = new Path();
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public DrawingView(Context context) {
super(context);
surfaceHolder = getHolder();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(Color.WHITE);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
if (surfaceHolder.getSurface().isValid()) {
Canvas canvas = surfaceHolder.lockCanvas();
path.lineTo(event.getX(), event.getY());
canvas.drawPath(path, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
break;
}
return true;
}
}
}