Android: Click image to darken effect with filter

Source: Internet
Author: User

To achieve a click on the image (ImageView) dimming effect, there is a simple way to say that the target image is set to the background image (SetBackground), Create a selector.xml file with a transparent picture in normal state, a brown translucent picture in a click, and set it as the source image of ImageView. This way, when you click ImageView, the source image changes transparency to darken the effect. However, this method has a drawback: because the source image has been limited to death, if we need to customize the background image, the target image is placed on the background image, it will not be possible. Here is a way to achieve the dimming effect by filtering and monitoring ontouchevent events.

The source code is as follows:

ImportAndroid.content.Context;ImportAndroid.graphics.Color;ImportAndroid.graphics.PorterDuff;Importandroid.graphics.drawable.Drawable;ImportAndroid.util.AttributeSet;Importandroid.view.MotionEvent;ImportAndroid.widget.ImageView;/*** @ClassName: Thumbnailview * @Description: ImageView to show light and shade changes (filter effects) when clicked *@authorLinj * @date 2015-1-6 pm 2:13:46 **/ Public classThumbnailviewextendsimageview{ PublicThumbnailview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub} @Override Public Booleanontouchevent (Motionevent event) {Switch(event.getactionmasked ()) { CaseMotionevent.action_down://set the filter in the press eventsetfilter ();  Break;  Casemotionevent.action_up://The Click event needs to be triggered manually due to capture of the touch eventPerformClick ();  CaseMotionevent.action_cancel://clear filters in cancel and up eventsremovefilter ();  Break; default:             Break; }        return true; }    /*** Set Filter*/    Private voidsetfilter () {//get the src picture of the setting firstDrawable drawable=getdrawable (); //when the SRC picture is null, get the background picture        if(drawable==NULL) {drawable=Getbackground (); }        if(drawable!=NULL){            //Set FilterDrawable.setcolorfilter (color.gray,porterduff.mode.multiply); }    }    /*** Clear Filter*/    Private voidremovefilter () {//get the src picture of the setting firstDrawable drawable=getdrawable (); //when the SRC picture is null, get the background picture        if(drawable==NULL) {drawable=Getbackground (); }        if(drawable!=NULL){            //Clear FilterDrawable.clearcolorfilter (); }    }}

Use the monitor down event to set the filter to monitor the up and cancel events to remove the filter to achieve the image dimming effect. But this method also has a very troublesome disadvantage: because of the interception of touch events, the onclick and Onlongclick events and many other events can not be triggered normally, as shown in the code above to manually trigger the Click event. When the view is the internal view of the ListView, the situation will be more cumbersome, so use caution and do not use this method in situations where complex interactions are required.

To modify the Add long press event:

 PackageCom.linj.camera.view;ImportAndroid.content.Context;ImportAndroid.graphics.Color;ImportAndroid.graphics.PorterDuff;Importandroid.graphics.drawable.Drawable;ImportAndroid.util.AttributeSet;ImportAndroid.view.GestureDetector;Importandroid.view.MotionEvent;ImportAndroid.widget.ImageView;/*** @ClassName: Filterimageview * @Description: ImageView to show light and shade changes (filter effects) when clicked *@authorLinj * @date 2015-1-6 pm 2:13:46 **/ Public classFilterimageviewextendsImageViewImplementsgesturedetector.ongesturelistener{/**Listening gestures*/     PrivateGesturedetector Mgesturedetector;  PublicFilterimageview (Context context, AttributeSet attrs) {Super(context, attrs); Mgesturedetector=NewGesturedetector (Context, This); } @Override Public Booleanontouchevent (Motionevent event) {//Cancel the filter in Cancel, take care not to capture the Cacncel event, and the Mgesturedetector has a capture operation for cancel//when you slide the GridView, the Abslistview blocks the move and up events and returns the cancel directly to the child control .        if(event.getactionmasked () = =motionevent.action_cancel)        {removefilter (); }        returnmgesturedetector.ontouchevent (event); }    /*** Set Filter*/    Private voidsetfilter () {//get the src picture of the setting firstDrawable drawable=getdrawable (); //when the SRC picture is null, get the background picture        if(drawable==NULL) {drawable=Getbackground (); }        if(drawable!=NULL){            //Set FilterDrawable.setcolorfilter (color.gray,porterduff.mode.multiply); }    }    /*** Clear Filter*/    Private voidremovefilter () {//get the src picture of the setting firstDrawable drawable=getdrawable (); //when the SRC picture is null, get the background picture        if(drawable==NULL) {drawable=Getbackground (); }        if(drawable!=NULL){            //Clear FilterDrawable.clearcolorfilter (); }} @Override Public BooleanOndown (motionevent e) {setfilter (); //This must return true, which means capturing this touch event        return true; } @Override Public voidonshowpress (motionevent e) {//TODO auto-generated Method Stub} @Override Public BooleanOnsingletapup (motionevent e) {removefilter ();            PerformClick (); return false; } @Override Public BooleanOnscroll (motionevent E1, motionevent E2,floatDistancex,floatDistancey) {        //TODO auto-generated Method Stub        return false; } @Override Public voidonlongpress (motionevent e) {//when Changan, manually trigger Changan eventPerformlongclick (); } @Override Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {        //TODO auto-generated Method Stub        return false; }}

Android: Click image to darken effect with filter

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.