[Android special effects] How does one add a mask to the ImageView, and when the image is clicked, it is black or white ,?

Source: Internet
Author: User

Basic Ideas:

Method 1. the mask can directly overlay a pure white or black View with alpha, and can directly overlay the FrameLayout layer in the ImageView coat. Its foreground (generally, you may only know the background, but framelayout and foreground) specify a selector (0x33000000 when pressed, that is,) in place.

Method 2: override imageView. When the status changes, invalidate (triggers onDraw), and 0x33000000 is drawn in onDraw Based on the status. Note that dispatchSetPressed can be triggered only when imageView. setClickable (true) or imageView. setOnClickListener is used.

@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the 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.