How does Android implement the click state of non local images

  • 2020-05-07 20:22:31
  • OfStack

For local images we can easily implement the click mode with selector.
However, in our project, a click-state implementation of non-local images still stumped many people; So write a blog to explain.
In fact, the click mode of Android non-local images is very simple, just need to change the Alpha value of ImageView when it is pressed.
example 1
Code snippet 1

 
View.OnTouchListener 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 2
 
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); 

The end!


Related articles: