IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Instancier vue java depuis l'XML


Sujet :

Android

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut Instancier vue java depuis l'XML
    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 au début testé uniquement le code pour faire la signature (avec un bitmap, canvas etc.), il marche bien. J'ai ensuite rajouté plusieurs onglets pour mon application est dans l'un d'eux, le code de la signature plus des boutons pour effacer et valider.

    Toute ma difficulté est de lier la vue créé dans le java qui permet de dessiner la signature et les boutons définis dans le XML. J'ai réussi à trouver plusieurs exemples et explications sur différents forum et fini par mettre en place une solution (avec le constructeur MyView(Context c, AttributeSet attrs), et la <view/> dans le XML )mais une erreurs persiste.

    Mon problème est que quand je teste avec le logiciel de virtualisation, l'application plante au moment ou je clic sur cette onglet et me met le message : Désolé, fermeture soudaine de l'application.

    Mon code java (Tab2.java) est :

    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
    122
    123
    124
    125
    126
    127
    128
    129
    130
     
    package andro.test.ids_telephone;
     
    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.graphics.Rect;
    import android.os.Bundle;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
     
    public class Tab2 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);
            }
     
       	 	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, 400, oldw, oldh);
                    mBitmap = Bitmap.createBitmap (w, 400, Bitmap.Config.ARGB_8888);
                   // mBitmap2 = Bitmap.createBitmap (mBitmap, 0, 0, 480, 600);
                    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.drawBitmap(mBitmap, new Rect(0,0,480,400), new Rect(0,0,480,400), 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;
            }     
    	}
     
     
     
     
    	private Paint mPaint;
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);
     
    	        mView = new MyView(this);
     
    	        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);
     
    	       setContentView(R.layout.onglet2);
     
    	}
    }
    Et le XML ( onglet2.xml) correspondant :

    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
     
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:id="@+id/linearLayoutOnglet2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" 
     >
         <Button
            android:id="@+id/effacer"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="42dp"
            android:layout_marginRight="60dp"
            android:text="effacer" />
     
        <Button
            android:id="@+id/valider"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="5dp"
            android:text="valider"/>
     
    	<view
    		class="andro.test.ids_telephone.MyView" 
    		android:id="@+id/signature"
    		android:layout_width="fill_parent"
    		android:layout_height="fill_parent"
    		/>
     
    </LinearLayout>

    Je fini par donner les erreures du LogCat :
    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
     
    08-30 14:21:39.952: D/AndroidRuntime(853): Shutting down VM
    08-30 14:21:39.952: W/dalvikvm(853): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
    08-30 14:21:39.952: E/AndroidRuntime(853): Uncaught handler: thread main exiting due to uncaught exception
    08-30 14:21:39.972: E/AndroidRuntime(853): java.lang.RuntimeException: Unable to start activity ComponentInfo{andro.test.ids_telephone/andro.test.ids_telephone.Tab2}: android.view.InflateException: Binary XML file line #26: Error inflating class andro.test.ids_telephone.MyView
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.widget.TabHost.setCurrentTab(TabHost.java:320)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:379)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.View.performClick(View.java:2364)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.View.onTouchEvent(View.java:4179)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.View.dispatchTouchEvent(View.java:3709)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.os.Looper.loop(Looper.java:123)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.ActivityThread.main(ActivityThread.java:4363)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at java.lang.reflect.Method.invokeNative(Native Method)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at java.lang.reflect.Method.invoke(Method.java:521)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at dalvik.system.NativeStart.main(Native Method)
    08-30 14:21:39.972: E/AndroidRuntime(853): Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class andro.test.ids_telephone.MyView
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.Activity.setContentView(Activity.java:1622)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at andro.test.ids_telephone.Tab2.onCreate(Tab2.java:129)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	... 30 more
    08-30 14:21:39.972: E/AndroidRuntime(853): Caused by: java.lang.ClassNotFoundException:andro.test.ids_telephone.MyView in loader dalvik.system.PathClassLoader@44e36a18
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.LayoutInflater.createView(LayoutInflater.java:466)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
    08-30 14:21:39.972: E/AndroidRuntime(853): 	... 39 more
    J’espère avoir été assez claire.

  2. #2
    Membre extrêmement actif
    Avatar de Ryu2000
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    9 604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 9 604
    Points : 18 520
    Points
    18 520
    Par défaut
    On dirait que la classe segula.sitrum.ids_telephone.MyView n'existe pas.

    Qu'est-ce qu'il y a à la ligne 129 de Tab2.java ?

    L'erreur doit venir du XML, on dirait :
    class="andro.test.ids_telephone.MyView".

    Est-ce que :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <andro.test.ids_telephone.MyView
    //
    />
    Fonctionnerait ?
    Keith Flint 1969 - 2019

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    A la ligne 129 : setContentView(R.layout.onglet2);

    Pardon pour le segula.sitrum.ids_telephone.MyView à la place de : andro.test.ids_telephone.MyView, c'est une petite modification de dernière minute que j'ai oublié de copier dans mon précédent poste. J'ai bien sur utilisé andro.test.ids_telephone.MyView dans tout mon programme. (Et j'ai corrigé le code du message précédent).

    J'ai donc bien dans le XML :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    <view
    		class="andro.test.ids_telephone.MyView"
    Qui correspond bien au package andro.test.ids_telephone suivi de ma classe MyView (qui extend View). C'est comme ca que c'était sur les différent exemples que j'ai vu.
    Vu que ma classe MyView est dans la classe Tab2 j'ai testé avec class="andro.test.ids_telephone.Tab2.MyView" plutôt que class="andro.test.ids_telephone.MyView". mais ça ne marche pas mieux.

    j'ai aussi testé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    <andro.test.ids_telephone.MyView
     
    />
    (je ne sais pas si les deux syntaxes reviennent au même) et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    <andro.test.ids_telephone.Tab2.MyView
    />
    pour faire tous les cas possible mais c'est toujours pas ça.

  4. #4
    Membre extrêmement actif
    Avatar de Ryu2000
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    9 604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 9 604
    Points : 18 520
    Points
    18 520
    Par défaut
    Je ne m'y connait pas trop sans ce domaine, une fois j'ai fais une classe qu'extendait ImageView, mais je m'en rappel pas du tout...

    Dans le package andro.test.ids_telephone t'as une classe MyView qu'extends quoi ?
    Keith Flint 1969 - 2019

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    Elle extend View :

    tab2.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    package andro.test.ids_telephone;
     
     
    public class Tab2 extends Activity {
     
     
    	private class MyView extends View {

  6. #6
    Membre extrêmement actif
    Avatar de Ryu2000
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    9 604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 9 604
    Points : 18 520
    Points
    18 520
    Par défaut
    Ah mais si c'est une private class ça ne peut pas fonctionner !

    Parce que là ça n'existe pas andro.test.ids_telephone.MyView à la limite :
    andro.test.ids_telephone.Tab2.MyView mais même pas puisque c'est privé !
    Keith Flint 1969 - 2019

  7. #7
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    Ok parfait c'est ça et ça marche bien maintenant

    Je n'ai pas pu juste changer "private class MyView extends View" en "public class MyView extends View"" car ça ne marchait ni avec "andro.test.ids_telephone.MyView" ni avec "andro.test.ids_telephone.Tab2.MyView", donc j'ai créé une nouvelle classe "public class MyView extends View" en dehors de la classe Tab2.

    Je donne le code final si ça peut en aider d'autre :

    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
     
    import android.app.Activity;
    import android.graphics.Paint;
    import android.os.Bundle;
     
    public class Tab2 extends Activity {
     
    	private MyView mView;
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);
     
    	        mView = new MyView(this);
    	       setContentView(R.layout.onglet2);
          }
    }
    MyView.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
     
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    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 {
     
        private Bitmap  mBitmap = null;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        private Paint mPaint;
        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);
     
        }
     
        @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, 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;
        }     
    }
    onglet2.xml
    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
     
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:id="@+id/linearLayoutOnglet2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
     
     >
     
    	  <andro.test.ids_telephone.MyView
     
    	android:id="@+id/signature"
    		android:layout_width="fill_parent" 
    	    android:layout_height="300dp"
    		/>
     
    	<Button
            android:id="@+id/effacer"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/signature"
            android:layout_marginLeft="42dp"
    		android:layout_marginRight="35dp"
     
            android:text="effacer" />
     
        <Button
            android:id="@+id/valider"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf ="@+id/effacer"
            android:layout_alignTop ="@+id/effacer"
            android:layout_marginLeft="38dp"
            android:text="valider"/>
     
    </RelativeLayout>
    Merci beaucoup pour ton aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. JNI -> Appeler méthode Java depuis C++ qui instancie un objet
    Par MilWolf dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 07/02/2015, 12h36
  2. Invoquer Java depuis une vue JSF
    Par splouf_operator dans le forum JSF
    Réponses: 16
    Dernier message: 12/09/2014, 10h19
  3. [XSD][JAVA] Valider un XML avec un XSD schéma
    Par vad dans le forum Valider
    Réponses: 1
    Dernier message: 17/08/2005, 11h47
  4. [SAX] Passer d'objet java en fichier XML?
    Par spoutyoyo dans le forum Format d'échange (XML, JSON...)
    Réponses: 15
    Dernier message: 05/01/2005, 08h31
  5. Lancement d'un programme java depuis un script php
    Par gexti dans le forum Développement Web en Java
    Réponses: 8
    Dernier message: 07/05/2004, 17h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo