android 图片点击变暗的另一种办法

来源:互联网 发布:linux安装软件 编辑:程序博客网 时间:2024/05/17 03:07


图片点击变暗的效果  一般情况下都是用两张图片 一张原图一张变暗后的图片  通过selector  android:state_pressed 判断是点击还是非点击来切换图片。

这个方法确实很好用,但是后来设想  很多地图图片都需要点击效果   如果用这个方法的话 那是不是需要往项目中添加大量重复的图片而仅仅是改变了明暗。而增加了项目的大小。

于是我决定用一张图片来实现变暗的效果    图片的ontouch事件中去处理  当action为down的时候让图片添加灰度, action为up的时候再改为white


代码如下:


import android.graphics.PorterDuff
final ImageView image = (ImageView) findViewById(R.id.image);        final Drawable drawable = getResources().getDrawable(R.drawable.abc);        image.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if(event.getAction() == MotionEvent.ACTION_DOWN){Log.e("suo", "down");drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);image.setBackgroundDrawable(drawable);}else if(event.getAction() == MotionEvent.ACTION_MOVE){Log.e("suo", "move");}else if(event.getAction() == MotionEvent.ACTION_UP){Log.e("suo", "up");drawable.setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);image.setBackgroundDrawable(drawable);}return false;}});


原创粉丝点击