Bonjour,
Je travail sur une application sur laquelle l'utilisateur pourrait faire une signature (comme pour recevoir un colis), et valider grâce à un bouton. J'ai réussi à faire le code pour faire la signature (avec un bitmap, canvas etc.) et à y ajouter le bouton pour effacer la signature (pour pouvoir la refaire si mal faite) et le code qui va avec.
Mon problème est que le signature s'efface uniquement quand on appuis sur le bouton effacer ET que l'on touche l'écran dans la zone de dessin (juste un appuis, pas besoin de retirer son doigt ni de le déplacer sur l'écran). C'est comme s'il fallait appuyer sur l'écran pour qu'il se réactualise et efface la signature précédente.
Comment faire pour effacer automatiquement le dessin lors de l'appuis sur le bouton (appel de ma méthode clear) sans avoir en plus à appuyer sur l'écran ?
Je vous donne mon code actuel :
MyView.java
Tab2.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 package andro.test.ids_telephone; 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.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class MyView extends View { public MyView mView; public Bitmap mBitmap = null; private static Canvas mCanvas; private static Path mPath; private static Paint mBitmapPaint; private static Paint mPaint; public static MotionEvent event1; public MyView(Context c) { super(c); } public MyView(Context c, AttributeSet attrs) { super(c, attrs); } public MyView(Context c, AttributeSet attrs, int defStyle) { super(c, attrs, defStyle); } @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); reset(); } @Override protected void onDraw(Canvas canvas) { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(2); canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); canvas.drawPath(mPath, mPaint); } private float mX; private float mY; private final float TOUCH_TOLERANCE = 4; private static final int BACKGROUND = Color.WHITE; 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 } @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; } private void clear() { Paint p = new Paint(); p.setColor(BACKGROUND); mCanvas.drawPaint(p); } }
J'ai fait plusieurs tests pour la méthode clear comme :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package andro.test.ids_telephone; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Tab2 extends Activity implements View.OnClickListener{ private MyView mView; Button effacer = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mView = new MyView(this); setContentView(R.layout.onglet2); effacer = (Button) findViewById(R.id.effacer); effacer.setOnClickListener(this); } public void onClick(View v) { mView.clear(); } }
ou
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 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 résultat est le même, ca efface seulement quand on appuis sur le bouton effacer et que l'on touche l’écran.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
Vu que l'écran s'efface au moment de l'appuis mais qu'il n'y a pas besoin de retirer son doigt ni de le déplacer sur l'écran, j'ai pensé à appeler la méthode touch_start() à la fin de clear() en espèrent que cela "réactualise" l'écran mais ça na rien changé.
J’espère avoir été assez claire (et pas trop long).
Partager