Android code-only addition and click effects

Source: Internet
Author: User

There are a lot of buttons in the project, and many Button cutting diagrams are configured at the same time. Is Selector annoying? using the following class, you can directly add the clicking effect for the Button. No need for multiple images, no need for Selector.

Usage: Button. setOnTouchListener (Effect_Click.getInstance ());

:

Source code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> class Effect_Click implements OnTouchListener {private static Effect_Click; public static Effect_Click getInstance () {if (Effect_Click = null) {Effect_Click = new Effect_Click ();} return Effect_Click;} private Effect_Click () {}@ Overridepublic boolean onTouch (View arg0, MotionEvent arg1) {if (arg1.getAction () = MotionEvent. ACTION_DOWN) {arg0.getBackground (). setAlpha (450);} else if (arg1.getAction () = MotionEvent. ACTION_UP) {arg0.getBackground (). setAlpha (999) ;}arg0.invalidate (); return false ;}}

Usage 1: Button. setOnTouchListener (Effect_Click.getInstance ());

Usage 2: You can perform secondary encapsulation. For example, you can implement your own MyButton and directly reference it in XML. in this way, as long as the background image is set for all the buttons in the project, it will naturally bring the click effect and no longer need to cut the image.

Usage 3: introduce others and add other effects to Drawable, such as adding borders and vibration animations. add multiple effects to buttons in batches and concisely with pure code.



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.