HSBImageView--android--可以设置HSB值的imageview

package guide.yunji.com.guide.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

import guide.yunji.com.guide.R;


public class HSBImageView extends AppCompatImageView {
    private static ColorMatrix colorMatrix = new ColorMatrix();
    /**
     * 色调,改变颜色
     */
    private static ColorMatrix hueMatrix = new ColorMatrix();
    /**
     * 饱和度,改变颜色的纯度
     */
    private static ColorMatrix saturationMatrix = new ColorMatrix();
    /**
     * 亮度,控制明暗
     */
    private static ColorMatrix brightnessMatrix = new ColorMatrix();

    private float hueValue = 0f;
    private float saturationValue = 1f;
    private float brightnessValue = 1f;

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

    public HSBImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HSBImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.HSBImageView);
        hueValue = array.getFloat(R.styleable.HSBImageView_hueValue, 0f);
        saturationValue = array.getFloat(R.styleable.HSBImageView_saturationValue, 1f);
        brightnessValue = array.getFloat(R.styleable.HSBImageView_brightnessValue, 1f);
        array.recycle();  //释放资源
        setHSB(hueValue, saturationValue, brightnessValue);
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        setHSB(hueValue, saturationValue, brightnessValue);
    }

    private void setHSB(float hueValue, float saturationValue, float brightnessValue) {
        //设置色相,为0°和360的时候相当于原图
        hueMatrix.reset();
        hueMatrix.setRotate(0, hueValue);
        hueMatrix.setRotate(1, hueValue);
        hueMatrix.setRotate(2, hueValue);

        //设置饱和度,为1的时候相当于原图
        saturationMatrix.reset();
        saturationMatrix.setSaturation(saturationValue);

        //亮度,为1的时候相当于原图
        brightnessMatrix.reset();
        brightnessMatrix.setScale(brightnessValue, brightnessValue, brightnessValue, 1);

        //将上面三种效果和选中的模式混合在一起
        colorMatrix.reset();
        colorMatrix.postConcat(hueMatrix);
        colorMatrix.postConcat(saturationMatrix);
        colorMatrix.postConcat(brightnessMatrix);

        setColorFilter(new ColorMatrixColorFilter(colorMatrix));
    }
}
<declare-styleable name="HSBImageView">
        <!--name:自定义属性名,format:自定义属性数据类型-->
        <attr name="hueValue" format="float"></attr>
        <attr name="saturationValue" format="float"></attr>
        <attr name="brightnessValue" format="float"></attr>
    </declare-styleable>
 <guide.yunji.com.guide.view.HSBImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:saturationValue="1.8"
        app:brightnessValue="1.0"
        android:id="@+id/shade"/>

可以设置HSB值的imageview

上一篇:解析RGB与HSB


下一篇:linux文件类型和重要命令(find ,tar,tr)