How does the "Android effect" add a mask to the ImageView, when clicked, Black, or white?

Source: Internet
Author: User

Basic ideas:

Method 1. Masks can be directly superimposed on a layer with alpha white, or pure black view, can be directly in the ImageView coat layer framelayout, its foreground (general classmate may only know background, In fact Framelayout also have foreground) given a selector (when pressed to 0x33000000, that is, Pan-black), in place.

Method 2. Rewrite the ImageView, invalidate (trigger OnDraw) when the state is changed, OnDraw draws 0x33000000 according to the pressed state. Note Imageview.setclickable (true), or Imageview.setonclicklistener, can trigger dispatchsetpressed.

@Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);//Take id//imgtest = (ImageView) Findviewbyid (r.id.imgtest); lomain = (relativelayout) Findviewbyid (R.id.lomain); Findviewbyid (R.ID.BTN1). Setonclicklistener (this); Findviewbyid (R.ID.BTN2). Setonclicklistener (this); Myimg myimg = new Myimg (this); Myimg.setimageresource (R.DRAWABLE.MEINV); myimg.setclickable (true); Lomain.addview ( MYIMG);} Private class Myimg extends ImageView {public myimg (context context) {super (context);} @Overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas); if (ispressed ()) Canvas.drawcolor (0x33000000);} @Overrideprotected void dispatchsetpressed (Boolean pressed) {//TODO auto-generated method stubsuper.dispatchsetpressed (pressed); invalidate ();}}





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.