I want to make the finger paint that have fix length. when I paint
more and more, It's removed the old paint and the length of the line
still the same as started.
This is my code:
package com.penhlenh.FingerPaint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class Main extends Activity {
private Paint mPaint;
private float mX, mY;
private float x;
private float y;
private static final float TOUCH_TOLERANCE = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
initialPaintStyle();
setContentView(new FingerPaint(this));
}
private void initialPaintStyle(){
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
}
class FingerPaint extends View {
private Path mPath;
private Canvas mCanvas;
private Bitmap mBitmap;
private Paint mBitmapPaint;
private float mStartX = 170;
private float mStartY = 650;
private float dx;
private float dy;
public FingerPaint(Context context) {
super(context);
mBitmap = Bitmap.createBitmap(480, 800,
Bitmap.Config.ALPHA_8);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPath.moveTo(mStartX, mStartY);
mPath.quadTo(120, 740, 40, 760);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private void touch_start(float x, float y) {
//mPath.reset();
mPath.moveTo(mStartX, mStartY);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
dx = Math.abs(x - mX);
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);
mCanvas.drawPath(mPath, mPaint);
//mPath.reset();
mStartX = mX;
mStartY = mY;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
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;
}
}
}
It draws with the finger properly but it's not remove the old line to
make the length the same as started.
how to remove the old paint? please help !!!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en