首页 > 系统 > Android > 正文

Android如何实现非本地图片的点击态

2019-10-24 21:10:21
字体:
来源:转载
供稿:网友
Android如何实现非本地图片的点击态,本文提供了详细的实现代码,需要了解的朋友可以参考下
 

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

代码片段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); 

结束!
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表