您的位置:首页 > 移动开发 > Android开发

Android 图片 滤镜效果 通过矩阵实现

2016-04-06 00:00 218 查看
然,看到头像点击要加滤镜效果,有个新奇 的做法是用setColorFilter();

/**
* Created by yx on 16/4/3.
*/public class DiscolorImageView extends ImageView{    /**
* 变暗
*/
private final float[] SELECTED_DARK = new float[]
{1, 0, 0, 0, -80,            0, 1, 0, 0, -80,            0, 0, 1, 0, -80,            0, 0, 0, 1, 0};    /**
* 变亮
*/

private final float[] SELECTED_BRIGHT = new float[]
{1, 0, 0, 0, 80,                    0, 1, 0, 0, 80,                    0, 0, 1, 0, 80,                    0, 0, 0, 1, 0};    /**
* 高对比度
*/

private final float[] SELECTED_HDR = new float[]
{5, 0, 0, 0, -250,                    0, 5, 0, 0, -250,                    0, 0, 5, 0, -250,                    0, 0, 0, 1, 0};    /**
* 高饱和度
*/
private final float[] SELECTED_HSAT = new float[]
{(float) 3, (float) -2, (float) -0.2, 0, 50,
-1, 2, -0, 0, 50,
-1, -2, 4, 0, 50,                    0, 0, 0, 1, 0};    /**
* 改变色调
*/
private final float[] SELECTED_DISCOLOR = new float[]
{(float) -0.5, (float) -0.6, (float) -0.8, 0, 0,
(float) -0.4, (float) -0.6, (float) -0.1, 0, 0,
(float) -0.3, 2, (float) -0.4, 0, 0,                    0, 0, 0, 1, 0};    public DiscolorImageView(Context context) {
super(context);        this.setOnTouchListener(VIEW_TOUCH_DISCOLOR);
}    public DiscolorImageView(Context context, AttributeSet attrs) {
super(context, attrs);        this.setOnTouchListener(VIEW_TOUCH_DISCOLOR);
}    public DiscolorImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);        this.setOnTouchListener(VIEW_TOUCH_DISCOLOR);
}    public OnTouchListener VIEW_TOUCH_DISCOLOR = new OnTouchListener() {

@Override        public boolean onTouch(View v, MotionEvent event) {            if (event.getAction() == MotionEvent.ACTION_DOWN) {
ImageView iv = (ImageView) v;
iv.setColorFilter(new ColorMatrixColorFilter(SELECTED_HDR));              //iv.setColorFilter(new ColorMatrixColorFilter(SELECTED_BRIGHT));
//iv.setColorFilter(new ColorMatrixColorFilter(SELECTED_HDR));
//iv.setColorFilter(new ColorMatrixColorFilter(SELECTED_HSAT));
//iv.setColorFilter(new ColorMatrixColorFilter(SELECTED_DISCOLOR));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
ImageView iv = (ImageView) v;
iv.clearColorFilter();
mPerformClick();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
ImageView iv = (ImageView) v;
iv.clearColorFilter();
}            return true;

}
};        private void mPerformClick() {
DiscolorImageView.this.performClick();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: