ひらい ぶらり Hi-Library

ぷろぐらみんぐについて。ときどきどうでもいいことについて。

ImageViewの明度、コントラスト、彩度、色相をObjectAnimatorでアニメーションする

タイトルのようなことをしたかったんですが、そんなプロパティはなかったのでImageViewを拡張してプロパティをつけたら出来ました。

↓のようなクラスを作って、xmlなり動的に生成するなりしたImageViewに対して操作をすれば期待した動きをしてくれる。

public class ExtendedPropatyImageView extends ImageView{

    private static final float[] defaultValues = {
            1, 0, 0, 0, 0,
            0, 1, 0, 0, 0,
            0, 0, 1, 0, 0,
            0, 0, 0, 1, 0
    };
    private int mBrightness = 0;
    private float mContrast = 0f;
    private float mSaturation = 0f;
    private float mHue = 0f;
    private int mRed = 0;
    private int mGreen = 0;
    private int mBlue = 0;

    public ExtendedPropatyImageView(Context context) {
        super(context);
    }

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

    public void setBrightness(int brightness) {
        float[] values = defaultValues;
        ColorMatrix matrix = new ColorMatrix();
        values[4] = values[9] = values[14] = brightness;
        setColorFilter(new ColorMatrixColorFilter(values));
        mBrightness = brightness;
    }

    public int getBrightness() {
        return mBrightness;
    }

    public void setContrast(float contrast) {
        float[] values = defaultValues;
        values[0] = values[6] = values[12] = contrast;
        setColorFilter(new ColorMatrixColorFilter(values));
        mContrast = contrast;
    }

    public float getContrast() {
        return mContrast;
    }

    public void setSaturation(float saturation) {
        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(saturation);
        setColorFilter(new ColorMatrixColorFilter(matrix));
        mSaturation = saturation;
    }

    public float getSaturation() {
        return mSaturation;
    }

    public void setHue(float hue) {
        float[] values = defaultValues;
        float a = Math.max(0, (float) Math.cos(hue / 360 * 2 * Math.PI));
        float b = Math.max(0, (float) Math.cos((hue - 120) / 360 * 2 * Math.PI));
        float c = Math.max(0, (float) Math.cos((hue - 240) / 360 * 2 * Math.PI));
        float sum = a + b + c;
        values[0] = values[6] = values[12] = a / sum;
        values[1] = values[7] = values[10] = b / sum;
        values[2] = values[5] = values[11] = c / sum;
        setColorFilter(new ColorMatrixColorFilter(values));
        mHue = hue;
    }

    public float getHue() {
        return mHue;
    }

    public void setRed(int red) {
        float[] values = defaultValues;
        values[4] = red;
        setColorFilter(new ColorMatrixColorFilter(values));
        mRed = red;
    }

    public int getRed() {
        return mRed;
    }

    public void setGreen(int green) {
        float[] values = defaultValues;
        values[9] = green;
        setColorFilter(new ColorMatrixColorFilter(values));
        mGreen = green;
    }

    public int getGreen() {
        return mGreen;
    }

    public void setBlur(int blue) {
        float[] values = defaultValues;
        values[14] = blue;
        setColorFilter(new ColorMatrixColorFilter(values));
        mBlue = blue;
    }

    public int getBlue() {
        return mBlue;
    }
}

アニメーションさせるときはこんな感じ

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ExtendedPropatyImageView image = (ExtendedPropatyImageView) findViewById(R.id.image_view);
    ObjectAnimator objectAnimator = ObjectAnimator.ofInt(image, "brightness", 0, 255);
    objectAnimator.setDuration(1000);
     objectAnimator.start();

}

後は値の型に合わせてofFloatにするなり、brightnessをhueやらに変えればそのプロパティを変更するアニメーションを実行することが出来た。

ColorMatrixクラスに彩度をセットするメソッドはあったけど、なんで他は無いんだろう・・・