有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓画布在画布上绘制路径,无需触摸方向

我构建这个类来在屏幕上绘制,以实现通过不同的绘制和路径创建的一些笔刷

当前正在基于用户触摸移动绘制正方形,但绘制路径正在按触摸的绘制方向旋转。我试图申请一个rect,但没有运气,我也没有找到关于这个现象的任何相关信息

编辑: This image is from current result

import 安卓.content.Context;
import 安卓.graphics.Bitmap;
import 安卓.graphics.Canvas;
import 安卓.graphics.Color;
import 安卓.graphics.Paint;
import 安卓.graphics.Path;
import 安卓.util.AttributeSet;
import 安卓.view.MotionEvent;

public class RachetBasic extends 安卓x.appcompat.widget.AppCompatImageView {
    private Canvas canvas;
    private Bitmap bitmap;
    private Paint bitmapPaint = new Paint(Paint.DITHER_FLAG);

    private Path squarePath = new Path();
    private Paint squarePaint = new Paint();

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

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

    public RachetBasic(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int width, int height, int oldw, int oldh) {
        super.onSizeChanged(width, height, oldw, oldh);

        squarePaint.setAntiAlias(true);
        squarePaint.setDither(true);

        squarePaint.setStyle(Paint.Style.STROKE);

        squarePaint.setStrokeCap(Paint.Cap.SQUARE);
        squarePaint.setStrokeWidth(80);

        squarePaint.setColor(Color.BLUE);

        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);

        canvas.drawPath(squarePath, squarePaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            squarePath.moveTo(x, y);
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            squarePath.lineTo(x, y);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            squarePath.lineTo(x, y);

            canvas.drawPath(squarePath, squarePaint);

            squarePath.reset();
        }

        invalidate();
        return true;
    }
}
public class MainActivity extends 安卓x.appcompat.app.AppCompatActivity {
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       setContentView(new RachetBasic(MainActivity.this));
    }
}

共 (0) 个答案