I work with painting. when drawing a line, it is perfectly drawn. when I draw a new line, the previously drawn line is removed from the view, I do not know why this is happening. Does anyone know please help me.
public class DrawView extends View implements OnTouchListener {// custom view private Canvas mCanvas; private Path mPath; private ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>(); private ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>(); public DrawView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); this.setOnTouchListener(this); mCanvas = new Canvas(); mPath = new Path(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); //do as user want } @Override protected void onDraw(Canvas canvas) { //onDraw method //DRAWING LINE................................ linearray[0]=stupx; linearray[1]=stupy; linearray[2]=upx; linearray[3]=upy; canvas.drawLines(linearray, mPaint); Log.d("METHOD", "ONDRAW LINE FIRED "+stupx+" "+stupy+""+upx+" "+upy); } private float mX, mY; private static final float TOUCH_TOLERANCE = 4; private void touch_start(float x, float y) { undonePaths.clear(); 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 touchline() { // TODO Auto-generated method stub Log.d("METHOD", "TOUCHLINE FIRED "); mPath.lineTo(mX, mY); // commit the path to our offscreen mCanvas.drawPath(mPath, mPaint); // kill this so we don't double draw newShapePaint = new Paint(mPaint); // Clones the mPaint object paths.add(new Pair<Path, Paint>(mPath, newShapePaint)); Log.d("TOUCH UP", "METHOD FIRED WITHOUT COLOR CHANGE"+_color); mPath = new Path(); } }
// onTouch event
@Override public boolean onTouch(View arg0, MotionEvent event) { x = event.getX(); y = event.getY(); upx=0f; upy=0f; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: stupx=event.getX(); stupy=event.getY(); touch_start(x, y); invalidate(); break; case MotionEvent.ACTION_MOVE: touch_move(x, y); invalidate(); break; case MotionEvent.ACTION_UP: Log.d("method", "touch up fired"); upx=event.getX(); upy=event.getY(); touchline();
source share