For local images, we can use selector to easily implement click states.
However, in our project, the implementation of click-state for non-local images is still difficult for many people. Therefore, we will write this blog to explain this.
In fact, the implementation principle of non-local images in Android is very simple. You only need to change the Alpha value of the displayed image when the imageview is pressed.
Example 1
CodeSegment 1
Copy code The Code is as follows: view. ontouchlistener = new view. ontouchlistener (){
@ Override
Public Boolean ontouch (view V, motionevent event ){
Imageview imgview = (imageview) V;
If (event. getaction () = motionevent. action_down ){
Imgview. setalpha (0xdf );
Imgview. invalidate ();
} Else if (event. getaction () = motionevent. action_up | event. getaction () = motionevent. action_cancel ){
Imgview. setalpha (0xff );
Imgview. invalidate ();
}
Return false;
}};
Code snippet 2Copy codeThe Code is as follows: View adsview = Inflater. Inflate (R. layout. ads_item, null );
Imageview img1 = (imageview) adsview. findviewbyid (R. layout. ads_item_left );
Imageview img2 = (imageview) adsview. findviewbyid (R. layout. ads_item_right );
Img1.setimageuri (uri1 );
Img2.setimageuri (uri2)
Img1.setontouchlistener (ontouchlistener );
Img2.setontouchlistener (ontouchlistener );
End!