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值的imageviewandroid

相關文章
相關標籤/搜索