111
public classFilterImageViewextendsandroid.support.v7.widget.AppCompatImageView {
publicFilterImageView(Context context) {
super(context);
}
publicFilterImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicFilterImageView(Context context, AttributeSet attrs,intdefStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 设置滤镜
*/
private voidsetFilter() {
//先获取设置的src图片
Drawable drawable = getDrawable();
//当src图片为Null,获取背景图片
if(drawable ==null) {
drawable = getBackground();
}
if(drawable !=null) {
//设置滤镜
drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
}
}
/**
* 清除滤镜
*/
private voidremoveFilter() {
//先获取设置的src图片
Drawable drawable = getDrawable();
//当src图片为Null,获取背景图片
if(drawable ==null) {
drawable = getBackground();
}
if(drawable !=null) {
//清除滤镜
drawable.clearColorFilter();
}
}
@Override
protected voiddrawableStateChanged() {
super.drawableStateChanged();
updateFilter();
}
private voidupdateFilter(){
if(isPressed()){
setFilter();
}else{
removeFilter();
}
}
}
网友评论