Android Pit Diary: Click to darken the effect of imageview implementation principle

Source: Internet
Author: User

Many times we use Imagview display pictures, whether it is Gilde,fresco, such as picture display frame, such as the setting center replacement avatar, grid albums Click Preview, select, etc., you will encounter the need to darken the interaction. Source Analysis
     The way we want to do this is to customize a ImageView, when you click on the image, there is a callback method to change the image of the filter or mask and so on.
     purposely went to see View.java's source code (ImageView inheritance view) to see if a callback function was available after the view was clicked.

View's Ontouchevent () method Case MotionEvent.ACTION_DOWN:mHasPerformedLongPress = false;  
              if (Performbuttonactionontouchdown (event)) {break;  
          //Walk up the hierarchy to determine if we re inside a scrolling container.  
          Boolean isinscrollingcontainer = Isinscrollingcontainer (); For views inside a scrolling container, delay the pressed feedback to//a short period in case this is a  
           Scroll.  
                 if (isinscrollingcontainer) {mprivateflags |= pflag_prepressed;  
                   if (Mpendingcheckfortap = = null) {Mpendingcheckfortap = new Checkfortap ();  
                   } postdelayed (Mpendingcheckfortap, Viewconfiguration.gettaptimeout ());  
                        else {//not inside a scrolling container, so show the feedback right away SetPressed (true);  
                    Checkforlongclick (0);  
 } break;

The meaning is Case:MotionEvent.ACTION_DOWN, when the event occurs, detects if the view is clicked, if clicked on the setpressed(True), and marks the status as clicked
The corresponding Case:MotionEvent.ACTION_UP, when releasing the hand, detects whether it is unpressed, if it is setpressed(false), and marks the status as not clicked.

Setpress (Boolean pressed) this method, defined as follows

  /**
     * Sets The pressed state for this view.
     * *
     @see #isClickable () *
     @see #setClickable (Boolean)
     *
     * @param pressed pass true to set the View ' s Inter NAL state to "pressed", or false to reverts
     * The View's internal state from        a previously set "pressed" state.
  
   */public
    void setpressed (Boolean pressed) {
        final Boolean Needsrefresh = pressed!= (mprivateflags & PFLA g_pressed) = = pflag_pressed);

        if (pressed) {
            mprivateflags |= pflag_pressed;
        } else {mprivateflags &= ~pflag_pressed
            ;
        }

        if (Needsrefresh) {
            refreshdrawablestate ();
        }
        dispatchsetpressed (pressed);
    }
  

This means that the view is invoked to change the state of the view, so we can make a fuss about this method and rewrite it. When the argument is true, use the color matrix Colormetrix to change the drawable filter, and when the argument is false, restore the image

/* Click Darken effect imageview*/public class Maskimageview extends ImageView {private Boolean toucheffect = true;
    Public final float[] bg_pressed = new float[] {1, 0, 0, 0,-50, 0, 1, 0, 0,-50, 0, 0, 1, 0,-50, 0, 0, 0, 1, 0};

    Public final float[] bg_not_pressed = new float[] {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0};
    Public Maskimageview {Super (context);
    Public Maskimageview (context, AttributeSet attrs) {Super (context, attrs);
    Public Maskimageview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Public Maskimageview (context context, AttributeSet attrs, int defstyleattr, int defstyleres) {super (con
    Text, Attrs, defstyleattr, defstyleres);
        @Override public void setpressed (Boolean pressed) {Updateview (pressed);
    super.setpressed (pressed); /** * Refresh BG and src * * @param pre on whether or not to press downssed * * private void Updateview (Boolean pressed) {//If no click Effect if (!toucheffect) {Retu
        Rn } if (pressed) {/** * To change the brightness of the picture by setting the filter * * This.setdrawingcacheenab
            LED (true);
            This.setcolorfilter (New Colormatrixcolorfilter (bg_pressed));
        This is SRC, background with getbackground () this.getdrawable (). Setcolorfilter (New Colormatrixcolorfilter (bg_pressed));
            }else{This.setcolorfilter (New Colormatrixcolorfilter (bg_not_pressed));
        This.getdrawable (). Setcolorfilter (New Colormatrixcolorfilter (bg_not_pressed)); }
    }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.