首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在手指android画布下画圆圈

,可以通过以下步骤实现:

  1. 创建一个自定义的View,并重写其onDraw方法。
  2. 在onDraw方法中,使用Canvas对象绘制圆圈。可以使用drawCircle方法来绘制圆形,传入圆心坐标和半径参数。
  3. 在View的onTouchEvent方法中,监听手指触摸事件。当手指按下时,获取手指触摸的坐标,并调用invalidate方法触发View的重绘。
  4. 在onDraw方法中,根据手指触摸的坐标绘制圆圈。可以使用Paint对象设置圆圈的颜色、边框宽度等属性。
  5. 当手指抬起时,停止绘制圆圈。

以下是一个示例代码:

代码语言:txt
复制
public class CircleView extends View {
    private float x, y; // 手指触摸的坐标
    private boolean isTouching = false; // 是否正在触摸

    public CircleView(Context context) {
        super(context);
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isTouching) {
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(5);

            canvas.drawCircle(x, y, 50, paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouching = true;
                x = event.getX();
                y = event.getY();
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                x = event.getX();
                y = event.getY();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                isTouching = false;
                invalidate();
                break;
        }
        return true;
    }
}

在使用这个自定义View时,可以将其添加到布局文件中,并设置其宽高等属性。例如:

代码语言:txt
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.CircleView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

这样,当手指在该View上触摸时,就会在手指触摸的位置绘制一个红色的圆圈。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券