gpt4 book ai didi

android - 在android中获取触摸形状

转载 作者:行者123 更新时间:2023-11-30 04:05:18 25 4
gpt4 key购买 nike

我正在创建一个应用程序,我想在我的屏幕上绘制触摸事件的形状。是否有专门获取触摸事件形状的应用程序?

非常感谢任何示例或代码 fragment 。

谢谢纳拉扬

最佳答案

此代码 fragment 应实现您想要的:

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;
}

}

}

关于android - 在android中获取触摸形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11779761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com