We know that using buttons or ImageView, you can add selector implementation styles to them.
For example:
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_pressed= "false" android:color= "#FFFFFF"/>
<item android:state_pressed= "true" android:color= "#FF0000"/>
</selector>
It is red when pressed and white when not pressed.
Let's say that when our button shows a picture that is a web image, but this button is a clickable button, we also need to add a style to the button, how will we write it?
If you understand iOS development, the iOS UIButton system button can be called a mask effect, meaning that when the button is pressed, add a mask to the button, this layer of the mask
is a translucent layer, non-pressed state, cancel out of the mask, you can implement the button's mask style.
To achieve this masking effect, we can think of a way
1. Use two imageview on the same layer, the bottom display picture of the ImageView, the upper layer of the custom selector style, the same can achieve the effect of the mask layer
2. Rewrite ImageView, ontouchevent overwrite event, when down, setalpha (transparent value), when up, we set it to Setalpha (0); Here is the code snippet for overriding ontouchevent in the inheritance Imageimage
public boolean ontouchevent (Motionevent event) {Boolean b = Super.ontouchevent (event); if (event.getaction () = = Motionevent.action_down) {Setalpha (0.5f);p ostinvalidate ();} Else{setalpha (1f);p ostinvalidate ();} return b;}
After you run the above code, you will find a problem, when pressed button will become translucent effect, but after the up event, the picture is still translucent, can not be restored to the original, this is why?
The reason is simple, because when executed to
after the event.getaction () ==motionevent.action_down, Setalpha () method is executed,returnthefalse, according to one of the article Event passing machines (Basic article)that, when down, if returnedfalse, the event will no longer be passed on, that is, the event will no longer be passed to up/move, etc., so once the Setalpha is set (0. 5f), Setalpha is no longer called (0), so the above problem arises, that is, the effect of bounce recovery cannot be achieved.
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);p ostinvalidate ();} Else{setalpha (1f);p ostinvalidate ();} return true;}
After running, it is found that the press becomes translucent and bounces back to normal.
But this code still has a problem, when we add Setonclicklistener () listener for this view, we find that we can't trigger the click event.
The reason is still very simple, because after rewriting ontouchevent, we did not call Super.ontouchevent, All view Click events are determined by the Super.ontouchevent event mechanism to determine the logic of the Click event, and here we invoke the call, that is, the event can not listen to the situation, 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);p ostinvalidate ();} Else{setalpha (1f);p ostinvalidate ();} return true;}
Running like this, everything's fine.
Summarize the above small examples:
- When overriding view.ontouchevent, you should call Super.ontouchevent, and if you do not call this method, system events should not be available, such as Click,longclick.
- After the ontouchevent is down, return false, the event will no longer continue to pass, that is, not to move, up, and so on, event delivery disappears.
- After the ontouchevent is down, return true, and the event will continue to be passed to subsequent events, namely move, up, and so on.
Event passing two (for simple, translucent effects)