[Android] Add the pressed animation to imagebutton to make it brighter or darker.

Source: Internet
Author: User

If imagebutton is used in Android,ProgramWhen you press the imagebutton file, you cannot see any effect.

There are two classic solutions on the Internet, one is to use XML, the other is to writeCode.

 

Here I would like to introduce another method to enable imagebutton to have the special effect of pressing. You only need to prepare a normal image without pressing the image.

Let's look at the sample code, create the touchlight and touchdark ontouchlistener, and set ontouchlistener for imagebutton. If touchlight is used, press the button to turn on. The other is to darken.

 

Import android. app. activity; import android. graphics. colormatrixcolorfilter; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; public class extends activity {public static final ontouchlistener touchlight = new ontouchlistener () {public final float [] bt_selected = new float [] {, 50,, 50, 0,}; public final float [] bt_not_selected = new float [] {, 0, 0, 0, 0, 1, 0}; @ overridepublic Boolean ontouch (view V, motionevent event) {If (event. getaction () = motionevent. action_down) {v. getbackground (). setcolorfilter (New colormatrixcolorfilter (bt_selected); V. setbackgrounddrawable (v. getbackground ();} else if (event. getaction () = motionevent. action_up) {v. getbackground (). setcolorfilter (New colormatrixcolorfilter (bt_not_selected); V. setbackgrounddrawable (v. getbackground () ;}return false ;}; public static final ontouchlistener touchdark = new ontouchlistener () {public final float [] bt_selected = new float, -50,0, 0,-50,0, 0,-50,0,}; public final float [] bt_not_selected = new float, ,}; @ overridepublic Boolean ontouch (view V, motionevent event) {If (event. getaction () = motionevent. action_down) {v. getbackground (). setcolorfilter (New colormatrixcolorfilter (bt_selected); V. setbackgrounddrawable (v. getbackground ();} else if (event. getaction () = motionevent. action_up) {v. getbackground (). setcolorfilter (New colormatrixcolorfilter (bt_not_selected); V. setbackgrounddrawable (v. getbackground () ;}return false ;};@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); imagebutton ib1, ib2; ib1 = (imagebutton) findviewbyid (R. id. imagebutton01); ib2 = (imagebutton) findviewbyid (R. id. imagebutton02); ib1.setontouchlistener (touchlight); ib2.setontouchlistener (touchdark );}}

The two float arrays in the Code store a color matrix. It doesn't matter if you don't know the color matrix. Just use this attachment and adjust the brightness, contrast, and other values, then copy the produced color matrix to the code.

Attachment: colormatrixdemo.swf

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.