Event transfer 2 (simple press translucent effect)

Source: Internet
Author: User

Event transfer 2 (simple press translucent effect)

We know that you can add Selector implementation styles to Button or ImageView.

For example:

 

 

It is red when pressed, and white when not pressed.

 

Suppose that the picture displayed by our button is a network image, but this button is a clickable button, then we also need to add a style for this button. How do we write it?

 

If you are familiar with IOS development, the UIButton system button of IOS can achieve a effect called the mask layer, which means that when you press the button, you can add a mask layer for the button, this masked layer

Is a translucent layer. When the screen is not pressed, you can cancel this layer to implement the button's mask style.

 

To achieve this effect, we can think of two methods.

1. Use two imageviews on the same layer. The image view is displayed on the underlying layer, and the selector style is customized on the upper layer to achieve the effect of the masked layer.

2. rewrite ImageView and rewrite the event in onTouchEvent. When it is Down, setAlpha (transparent value). When it is Up, set it to setAlpha (0, the following is the code segment that inherits the ImageImage to override onTouchEvent.

 

public boolean onTouchEvent(MotionEvent event) {boolean b = super.onTouchEvent(event);if(event.getAction()==MotionEvent.ACTION_DOWN){setAlpha(0.5f);postInvalidate();}else{setAlpha(1f);postInvalidate();}return b;}


 

 

 

After you run the above Code, you will find a problem. When you press the button, it will become translucent, but after the Up event is popped Up, the image is still translucent and cannot be restored. Why?

The reason is very simple, because when

Event. getAction () = MotionEvent. after the ACTION_DOWN and setAlpha () methods are executed, return false. According to one of the event transfer machines in the article (basic), we can see that if false is returned after the event is Down, the event will no longer be transmitted, that is, the event will not be passed to Up/move, etc. So once setAlpha (0.5f) is set, setAlpha (0) will not be called ), therefore, the above problem occurs, that is, the recovery effect cannot be restored.

 

So I made the following changes:

 

public boolean onTouchEvent(MotionEvent event) {//boolean b = super.onTouchEvent(event);if(event.getAction()==MotionEvent.ACTION_DOWN){setAlpha(0.5f);postInvalidate();}else{setAlpha(1f);postInvalidate();}return true;}

After running, it is found that the press becomes translucent, And the pop-up is back to normal.

 

 

However, this code still has a problem. When we add a setOnclickListener () Listener for this View, we find that the click event cannot be triggered. Why?

 

The reason is still very simple, because after the onTouchEvent is rewritten, we didn't call super. onTouchEvent, And the Click events of all views are composed of super. onTouchEvent's event mechanism determines the logic of click events. Here we call the event so that the event cannot be monitored. So I modified the Code as follows:

 

public boolean onTouchEvent(MotionEvent event) {//boolean b = super.onTouchEvent(event);super.onTouchEvent(event);if(event.getAction()==MotionEvent.ACTION_DOWN){setAlpha(0.5f);postInvalidate();}else{setAlpha(1f);postInvalidate();}return true;}

In this way, everything is normal.

 

 

To sum up the above small examples:

 

When you override View. onTouchEvent, you should call super. onTouchEvent. If you do not call this method, all system events, such as click and longclick, cannot be used. After the onTouchEvent is Down and return false, the event will not be passed any more, that is, it will not be passed to move or up, and the event will also disappear. After the onTouchEvent is Down, return true. The event will be passed to subsequent events, such as move and up.

 

 

 

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.