4.29
所花时间(包括上课):2
打码量(行):200
博客量(篇):1
了解到知识点:学习触摸事件
public class TouchableView extends View {
private float lastX, lastY;
private Paint paint;
public TouchableView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 手指按下时的处理
lastX = currentX;
lastY = currentY;
break;
case MotionEvent.ACTION_MOVE:
// 手指移动时的处理
float dx = currentX - lastX;
float dy = currentY - lastY;
// 重新绘制视图
invalidate();
lastX = currentX;
lastY = currentY;
break;
case MotionEvent.ACTION_UP:
// 手指抬起时的处理
break;
}
return true; // 返回true表示已处理触摸事件
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(lastX, lastY, 50, paint); // 绘制圆形
}
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/18138349
千万千万赵千万