gpt4 book ai didi

android - android中没有油漆

转载 作者:行者123 更新时间:2023-11-29 01:59:12 26 4
gpt4 key购买 nike

我正在编写用于签名的迷你应用程序,用户使用手指进行绘画。

我在函数 onPaint 中绘制但什么也没有。我不明白,我调试了但没有修复它。我明白了,如果我在函数 onCreate 中绘制就可以了。

我想在 onPaint 中绘制。

public class Main extends Activity{
ImageView drawingImageView;
float downx = 0;
float downy = 0;
float upx = 0;
float upy = 0;
private Bitmap bitmap;
private Canvas canvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

drawingImageView = (ImageView) this.findViewById(R.id.imageView1);
bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);

mBitmapPaint = new Paint(Paint.DITHER_FLAG);

mPath = new Path();

mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setTextSize(20);
mPaint.setTypeface(Typeface.DEFAULT);

canvas.drawLine(0, 0, 200, 200, mPaint);
}


protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(bitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
canvas.drawLine(0, 0, 100, 100, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
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;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
canvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
onDraw(canvas);
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
onDraw(canvas);
break;
case MotionEvent.ACTION_UP:
touch_up();
onDraw(canvas);
break;
}
return true;
}
}

最佳答案

在所有绘图操作之后调用 canvas.restore();

关于android - android中没有油漆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13527798/

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