Aller au contenu

Effacer un dessin


cyrilbouisson@gmail.com

Recommended Posts

Bonjour

Voici mon code permettant de dessiner :

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

public class Signature extends Activity {	

private class MyView extends View {

   	private Bitmap  mBitmap = null;
   	private Canvas  mCanvas;
   	private Path	mPath;
   	private Paint   mBitmapPaint;

   	public MyView(Context c) {
       	super(c);
   	}

   	@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);
       	mPath = new Path();
       	mBitmapPaint = new Paint(Paint.DITHER_FLAG);
   	}

   	@Override
   	protected void onDraw(Canvas canvas) {
       	canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
       	canvas.drawPath(mPath, 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
       	mCanvas.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);
               	invalidate();
               	break;
           	case MotionEvent.ACTION_MOVE:
               	touch_move(x, y);
               	invalidate();
               	break;
           	case MotionEvent.ACTION_UP:
               	touch_up();
               	invalidate();
               	break;
       	}
       	return true;
   	}

   	public void clear() {
   	}
}


private MyView mView;
private Paint mPaint;

@Override
protected void onCreate(Bundle savedInstanceState) {
   	super.onCreate(savedInstanceState);

   	mView = new MyView(this);
   	setContentView(mView);

   	mPaint = new Paint();
   	mPaint.setAntiAlias(true);
   	mPaint.setDither(true);
   	mPaint.setColor(0xFF000000);
   	mPaint.setStyle(Paint.Style.STROKE);
   	mPaint.setStrokeJoin(Paint.Join.ROUND);
   	mPaint.setStrokeCap(Paint.Cap.ROUND);
   	mPaint.setStrokeWidth(2);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
   	super.onCreateOptionsMenu(menu);

   	menu.add(0, 1, 0, "Valider");
   	menu.add(0, 2, 0, "Effacer");

   	return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
   	super.onPrepareOptionsMenu(menu);
   	return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   	mPaint.setXfermode(null);
   	mPaint.setAlpha(0xFF);

   	switch (item.getItemId()) {
       	case 1:
       		mPaint.setColor(0xFFFF0000);
           	break;

       	case 2:
       		mView.clear();
       		break;
   	}

   	return super.onOptionsItemSelected(item);
}
}

J'aimerais effacer le dessin en cours grâce à la méthode clear. J'ai fait pour l'instant ceci :

    	public void clear() {
   		int w = mBitmap.getWidth();
   		int h = mBitmap.getHeight();

   		mBitmap.recycle();

       	mBitmap = Bitmap.createBitmap (w, h, Bitmap.Config.ARGB_8888);
       	mCanvas = new Canvas(mBitmap);
       	mPath = new Path();
       	mBitmapPaint = new Paint(Paint.DITHER_FLAG);

   	}

Mais le dessin s'efface uniquement quand je réalise une pression sur mon écran...

Comment faire pour effacer automatiquement le dessin lors de l'appel de ma méthode clear ? Y'a-t-il une autre méthode ?...

Merci d'avance

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...