本人博客原文
对于本地图片我们可以通过selector来轻松的实现点击态。
但是在我们的项目中,一个关于对非本地图片的点击态实现还是难倒了不少人;因此专门写本博文来说明。
实际上Android中非本地图片的点击态起实现原理很简单,只需要在ImageView被按下时,改变其显示图片的Alpha值就可以了。
示例1
代码片段1
View.OnTouchListener onTouchListener =new View.OnTouchListener(){@Overridepublic 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;}};
代码片段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);
结束!