logo头像
Snippet 博客主题

ImageView控件开发滤镜效果

本文于2085天之前发表,文中内容可能已经过时。

实现过程说明:
使用设置图片对象的ColorFilter属性,把ColorMatrixColorFilter设置灰度通道,传递到ColorFilter属性中
注意两点:1 图片滤镜效果,只是在图层上面 加了一层效果,不是对图片的实际修改
\如果对单个图片加滤镜效果后,其他地方使用到这个图片也会变成带滤镜效果。
下面是实际实现代码
[code lang=”java”]
public final float[] BT_SELECTED = new float[] {1,0,0,0,99,0,1,0,0,99,0,0,1,0,99,0,0,0,1,0};
public final float[] BT_NOT_SELECTED = new float[] {1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0};;
public final static float[] BT_SELECTED1 = new float[] {
.338f, 0.339f, 0.332f, 0, 0,
.338f, 0.339f, 0.332f, 0, 0,
.338f, 0.339f, 0.332f, 0, 0,
, 0, 0, 1, 0
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView ib2;
ib1 = (ImageView) findViewById(R.id.imageViewt);
ib2 = (ImageView) findViewById(R.id.imageView2);

ib1.setOnTouchListener(new ImageView.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {

ib1.setImageResource(R.drawable.icon1);
ib1.getDrawable().setColorFilter(
new ColorMatrixColorFilter(BT_SELECTED));

ib1.setImageDrawable(ib1.getDrawable());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
ib1.getDrawable().clearColorFilter();

ib1.getDrawable().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
ib1.setImageResource(R.drawable.icon2);
}
return false;
}
});

}
[/code]

上一篇