Original blog
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 1Code 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);
End!