2013-04-08 2 views
0

J'essaie de faire un effet de clic dans ma classe ImageView personnalisée AvatarView.Image avec effet de clic

J'utilise ce code:

 // **** Listener onTouch ****. 

    // Añadimos un listener para su pulsación. 
    this.setOnTouchListener(new OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent event) { 

      switch (event.getAction()) { 

       case MotionEvent.ACTION_DOWN: 

        // Establecemos un filtrado de color. 
        AvatarView.this.setColorFilter(0xe0f47521, PorterDuff.Mode.SRC_ATOP); 
        AvatarView.this.invalidate(); 
        break; 

       case MotionEvent.ACTION_UP: 

        // Eliminamos el filtrado de color. 
        //AvatarView.this.clearColorFilter(); 
        AvatarView.this.setColorFilter(null); 
        AvatarView.this.invalidate(); 
        break; 


      } 

      return false; 
     } 
    }); 

Ce code fonctionne parfaitement dans mon téléphone 2.3, mais il échoue dans ma tablette 4.1 puisque l'image disparaît et ne réapparaît pas.

Je n'ai trouvé aucune solution. Une idée?

Merci.

Répondre

0

Enfin, j'ai trouvé la solution: il faut prendre la drawable:

 // **** Listener onTouch ****. 

    // Añadimos un listener para su pulsación. 
    this.setOnTouchListener(new OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent event) { 

      switch (event.getAction()) { 

       case MotionEvent.ACTION_DOWN: 

        // Establecemos un filtrado de color. 
        AvatarView.this.getDrawable().setColorFilter(0xe0f47521, PorterDuff.Mode.SRC_ATOP); 
        AvatarView.this.invalidate(); 
        break; 

       case MotionEvent.ACTION_UP: 

        // Eliminamos el filtrado de color. 
        AvatarView.this.getDrawable().clearColorFilter(); 
        AvatarView.this.invalidate(); 
        break; 

       case MotionEvent.ACTION_CANCEL: 

        // Eliminamos el filtrado de color. 
        AvatarView.this.getDrawable().clearColorFilter(); 
        AvatarView.this.invalidate(); 
        break; 

      } 

      return false; 
      //return true; 
     } 
    }); 

Maintenant, il fonctionne très bien.

Quelqu'un peut-il me dire si je devrais retourner vrai ou faux? Quelle est la différence?

Merci.

+1

false signifie que l'événement tactile n'a pas été consommé et sera donc propagé aux vues sous-jacentes. Vrai consomme l'événement afin que les enfants ne soient pas informés de l'événement – AndacAydin