Android获取网络图片的HSB值,HSBImageView--android--可以设置HSB值的imageview

忘是亡心i 2022-10-05 07:54 163阅读 0赞

packageguide.yunji.com.guide.view;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.ColorMatrix;importandroid.graphics.ColorMatrixColorFilter;importandroid.graphics.drawable.Drawable;importandroid.support.annotation.Nullable;importandroid.support.v7.widget.AppCompatImageView;importandroid.util.AttributeSet;importguide.yunji.com.guide.R;public class HSBImageView extendsAppCompatImageView {private static ColorMatrix colorMatrix = newColorMatrix();/*** 色调,改变颜色*/

private static ColorMatrix hueMatrix = newColorMatrix();/*** 饱和度,改变颜色的纯度*/

private static ColorMatrix saturationMatrix = newColorMatrix();/*** 亮度,控制明暗*/

private static ColorMatrix brightnessMatrix = newColorMatrix();private float hueValue =0f;private float saturationValue =1f;private float brightnessValue =1f;publicHSBImageView(Context context) {super(context);

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

}public HSBImageView(Context context, AttributeSet attrs, intdefStyleAttr) {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);

}

@Overridepublic void setImageResource(intresId) {super.setImageResource(resId);

setHSB(hueValue, saturationValue, brightnessValue);

}private void setHSB(float hueValue, float saturationValue, floatbrightnessValue) {//设置色相,为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(newColorMatrixColorFilter(colorMatrix));

}

}

发表评论

表情:
评论列表 (有 0 条评论,163人围观)

还没有评论,来说两句吧...

相关阅读