ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-10-09 18:52:43  阅读:329  来源: 互联网

标签:HSB ColorMatrix HSBImageView hueValue imageview import android brightnessValue


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

标签:HSB,ColorMatrix,HSBImageView,hueValue,imageview,import,android,brightnessValue
来源: https://www.cnblogs.com/dongweiq/p/11643645.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有