Bonjour,
mon problème doit être connu mais je ne trouve rien sur le net.
J'affiche un textView et quand on clique dessus avec le doigt, d'autres texViews apparaissent (animation)
J'utilise pour çaEn même temps j'aimerai que l'utilisateur puisse dessiner avec le doigt (qu'on puisse voir la trace du doigt, je teste en traçant ici des segments)
Code : Sélectionner tout - Visualiser dans une fenêtre à part onTouch(View view, MotionEvent motionEvent)
Pour ce faire, j'utilise un canvas dans une vue personnalisées etMais je n'arrive pas à avoir les deux ensemble :1) l'utilisateur dessine et 2) le déclenchement de l'animation quand le doigt passe sur le textView.
Code : Sélectionner tout - Visualiser dans une fenêtre à part onTouchEvent(MotionEvent event)
D'après mes recherches onTouchEvent est executée avant onTouch : on touche l'écran avant de toucher le textView.
Donc j'ai envie que onTouchEvent renvoie FALSE pour que le toucher ne soit pas consommé et que onTouch le voit. Ça fonctionne saut qu'alors onTouchEvent ne fonctionne plus et on ne peut plus dessiner.
Pour être précis ACTION_DOWN est réalisé mais ACTION_MOVE et ACTION_UP non car avec FALSE, pour onTouch, le doigt n'est plus sur l'écran.
J'ai lu qu'on pouvait utiliser performClick() mais je n'ai pas compris comment.
Je vous remercie pour votre attention.
Voici le code des 3 fichiers :
MainActivity.java
Code Java : 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 public class MainActivity extends AppCompatActivity { int hauteurEcran,largeurEcran; // En pixels int rayon,xcentre,ycentre,nombreLettres,tailleLettres; TextView tv,lettres[] = new TextView[10]; TranslateAnimation animation[] = new TranslateAnimation[10]; ConstraintLayout constraintLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); hauteurEcran = size.y; largeurEcran = size.x; tailleLettres = largeurEcran/10; rayon = (int)(hauteurEcran/4*0.8); xcentre = (int)(largeurEcran*0.5-tailleLettres/2); ycentre = (int)(hauteurEcran*0.75-tailleLettres); tv = findViewById(R.id.tv); tv.setX(xcentre); tv.setY(ycentre); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40); nombreLettres =7; constraintLayout = findViewById(R.id.cl); for(int i=0;i<nombreLettres;i++){ lettres[i] = new TextView(this); constraintLayout.addView(lettres[i]); lettres[i].setVisibility(View.GONE); lettres[i].setText(String.valueOf(i)); lettres[i].setBackgroundResource(R.drawable.rouge); lettres[i].getLayoutParams().width = tailleLettres; lettres[i].getLayoutParams().height = tailleLettres; lettres[i].setGravity(Gravity.CENTER | Gravity.CENTER); lettres[i].setTypeface(null, Typeface.BOLD); lettres[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 40); lettres[i].setId(i); lettres[i].setOnTouchListener(touchListener); } tv.setOnTouchListener(touchListener); } private View.OnTouchListener touchListener = new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (view == tv){ for(int i=0;i<nombreLettres;i++){ lettres[i].setX((float)(xcentre)); lettres[i].setY((float)(ycentre)); animation[i] = new TranslateAnimation(0,(float)(rayon*Math.cos(2*Math.PI*i/nombreLettres)),0,(float)(-rayon*Math.sin(2*Math.PI*i/nombreLettres))); lettres[i].setVisibility(View.VISIBLE); animation[i].setDuration(2000); animation[i].setFillAfter(true); animation[i].setFillEnabled(true); lettres[i].startAnimation(animation[i]); } } return true; } }; }
activity_main.xml
Code XML : 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 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/cl" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@drawable/app_background"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <fr.padrixe.devinelesmots.DrawingView android:id="@+id/myDrawingView" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
DrawingView.java
Code Java : 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 public class DrawingView extends View { Paint myPaint; int debutX=0; int debutY=0; int finX=0; int finY=0; Context c; public DrawingView(Context context, AttributeSet attributeSet) { super(context,attributeSet); myPaint = new Paint(); myPaint.setAntiAlias(true); myPaint.setDither(true); myPaint.setColor(Color.RED); myPaint.setStyle(Paint.Style.FILL_AND_STROKE); myPaint.setStrokeJoin(Paint.Join.ROUND); myPaint.setStrokeCap(Paint.Cap.ROUND); myPaint.setStrokeWidth(20); c=context; } @Override protected void onDraw(Canvas canvas) { canvas.drawLine(debutX, debutY, finX, finY, myPaint); // pour tester invalidate(); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Toast.makeText(c.getApplicationContext(),"ACTION_DOWN",Toast.LENGTH_SHORT).show(); // seul toast visible avec false debutX = (int) event.getX(); debutY = (int) event.getY(); finX = (int) event.getX(); finY = (int) event.getY(); invalidate(); break; case MotionEvent.ACTION_MOVE: Toast.makeText(c.getApplicationContext(),"ACTION_MOVE",Toast.LENGTH_SHORT).show(); finX = (int) event.getX(); finY = (int) event.getY(); invalidate(); break; case MotionEvent.ACTION_UP: Toast.makeText(c.getApplicationContext(),"ACTION_UP",Toast.LENGTH_SHORT).show(); invalidate(); break; } return false; } }
Partager