我正在为签名编写迷你应用程序,用户使用手指进行绘画。
我画了功能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;
}
}