gpt4 book ai didi

android - 如何绘制平滑的位图路径,没有任何中断

转载 作者:行者123 更新时间:2023-11-29 01:34:18 25 4
gpt4 key购买 nike

我正在开发一个绘图应用程序,我想绘制一个位图路径我的代码是这样的:

	private void onCanvasInitialization() {
// Main_Activity.paintButton.setEnabled(true);

mPaint = new Paint();
BlurMaskFilter bmf = new BlurMaskFilter(2, Blur.OUTER);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setFilterBitmap(true);
mPaint.setColor(Main_Activity.colorchanger);
mCanvas = new Canvas();
mPath = new Path();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setMaskFilter(bmf);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setAlpha(255);
bmp = Bitmap.createScaledBitmap(bmp, 30, 30, false);
bmp = bmp.copy(Config.ARGB_8888, true);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

mCanvas = new Canvas(mBitmap);
}

public boolean onTouch(View arg0, MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (true)
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}

private void touch_start(float x, float y) {
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Random rnd = new Random();
mPaint.setColor(Color.rgb(rnd.nextInt(256), rnd.nextInt(256),
rnd.nextInt(256)));

mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}

private void touch_move(float x, float y) {

float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
// mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);

mX = x;
mY = y;
arrX[i] = mX;
arrY[i] = mY;
i++;
}
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int k = 0; k < i; k++) {
canvas.drawBitmap(bmp, arrX[k], arrY[k], mPaint);

}
}

由此我可以在 Canvas 上绘制位图,但流程不流畅,它看起来不像一条线。两个连续的位图之间有间隙。我希望它看起来像一条路径。

最佳答案

为什么在每个 case: 中调用 invalidate() 方法,尝试调用它 ot switch。

例如

public boolean onTouch(View arg0, MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (true)
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
break;
case MotionEvent.ACTION_UP:
touch_up();
break;
}
invalidate();
return true;
}

关于android - 如何绘制平滑的位图路径,没有任何中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29408152/

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