Android UI之ImageVIew图片处理相关

1、简介

本篇博客用于记录日常开发中常用的ImageView的处理,包括加载以及一些高性能的处理方案,自己做一份记录的同时也希望可以帮到和我一样处于学习的初级开发者。

2、具体场景与使用a

a. 仿微信实现点击图片时图片变暗的效果

想必大家使用微信的时候都有留意到,你不管点击什么样的图片那么都会在你摁下的时候有一个灰度的图片效果,而在抬起时背景又恢复了,其实这个用户体验很好,这和我么设置按钮的点击背景切换的初衷是一样的,即提示用户咱们这个控件是被你点击了的,是的,还有动起来的效果总是很美好。

这里重点是图片的灰度效果: 和容易想到在手指按下的时候背景变灰,在手指抬起的时候背景变成透明的。

下面来看我们的自定义控件:

/**
 * des:实现图像根据按下抬起动作变化颜色
 * on 2016.07.11:14
 * @author zxl
 */
public class ColorFilterImageView extends
        android.support.v7.widget.AppCompatImageView
        implements OnTouchListener {
    public ColorFilterImageView(Context context) {
        super(context);
        init();
    }

    public ColorFilterImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ColorFilterImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:  // 按下时图像变灰
                Log.e("事件","摁下");
                setColorFilter(Color.GRAY, Mode.MULTIPLY);
                break;
            case MotionEvent.ACTION_UP:   // 手指离开或取消操作时恢复原色
                Log.e("事件","抬起");
                setColorFilter(Color.TRANSPARENT);
            case MotionEvent.ACTION_CANCEL:
                Log.e("事件","取消");
                setColorFilter(Color.TRANSPARENT);
                break;
            default:
                break;
        }
        return false;
    }
}

是不是很简单啊,设置灰度就只需要调用 AppcomtImageView的setColorFilter即可。下面给出方法。

    public final void setColorFilter(int color) {
        setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    }

    public void setColorFilter(ColorFilter cf) {
        if (mColorFilter != cf) {
            mColorFilter = cf;
            mHasColorFilter = true;
            mColorMod = true;
            applyColorMod();
            invalidate();
        }
    }

    private void applyColorMod() {
        // Only mutate and apply when modifications have occurred. This should
        // not reset the mColorMod flag, since these filters need to be
        // re-applied if the Drawable is changed.
        if (mDrawable != null && mColorMod) {
            mDrawable = mDrawable.mutate();
            if (mHasColorFilter) {
                mDrawable.setColorFilter(mColorFilter);
            }
            mDrawable.setXfermode(mXfermode);
            mDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8);
        }
    }

 具体使用如下:

    <com.example.mydairytestproject.colorFliter.ColorFilterImageView
        android:id="@+id/filter"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:src="@drawable/sky"
        android:layout_width="200dp"
        android:layout_height="200dp" />
        mColorFilterImageView = (ColorFilterImageView) findViewById(R.id.filter);
        mColorFilterImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ImageFilterActivity.this, "点击", Toast.LENGTH_SHORT).show();
            }
        });

猜你喜欢

转载自blog.csdn.net/crazyZhangxl/article/details/81707215