根据进度算颜色值

来源:互联网 发布:松江报警主机怎样编程 编辑:程序博客网 时间:2024/06/07 15:38

调用

第一步:获取当前进度的颜色:int gradient = getGradient(0.7f, Color.BLACK, Color.BLUE);//-16777038int evaluate = evaluate(0.7f, Color.BLACK, Color.BLUE);//-16777038int gradient = getGradient(0.3f, Color.parseColor("#e5e5e5"), Color.parseColor("#ff8041"));//-1259596int evaluate = evaluate(0.3f, Color.parseColor("#e5e5e5"), Color.parseColor("#ff8041"));//-1259596int gradient = getGradient(0.5f, Color.argb(127, 255, 0, 0), Color.argb(234, 123, 10, 1));//-1262680832int evaluate = evaluate(0.5f, Color.argb(127, 255, 0, 0), Color.argb(234, 123, 10, 1));//-1262680832int gradient = getGradient(0.9f, 0xaaff0000, 0xaa00aa99);//-1441097335int evaluate = evaluate(0.9f, 0xaaff0000, 0xaa00aa99);//-1441097335第二步:设置颜色:mPaint.setColor(gradient);mPaint.setColor(evaluate);

注意:此处得到的结果gradient、evaluate为十进制数据(ColorInt)。

可以将这个十进制数据(ColorInt)转成十六进制(颜色),再在开头加个”#”,即为常见的颜色值,如下:

String hexString = Integer.toHexString(gradient);//aa1a9989tv.setBackgroundColor(Color.parseColor("#" + hexString));//#aa1a9989

参考:颜色的介绍

方法一:

改造自android.animation.ArgbEvaluator类:

  /**     * @param fraction   当前进度[0...1]     * @param startValue 起始颜色     * @param endValue   结束颜色     * @return 颜色值     */ public int evaluate(float fraction, int startValue, int endValue) {        int startA = (startValue >> 24) & 0xff;        int startR = (startValue >> 16) & 0xff;        int startG = (startValue >> 8) & 0xff;        int startB = startValue & 0xff;        int endA = (endValue >> 24) & 0xff;        int endR = (endValue >> 16) & 0xff;        int endG = (endValue >> 8) & 0xff;        int endB = endValue & 0xff;        return ((startA + (int) (fraction * (endA - startA))) << 24) |                ((startR + (int) (fraction * (endR - startR))) << 16) |                ((startG + (int) (fraction * (endG - startG))) << 8) |                ((startB + (int) (fraction * (endB - startB))));    }

方法二:

参考:自定义View之渐变圆环进度条

 /**     * @param fraction   当前进度[0...1]     * @param startColor 起始颜色     * @param endColor   结束颜色     * @return 颜色值     * 0xFFFF0000:00:0,FF:255     */    public static int getGradient(float fraction, int startColor, int endColor) {        if (fraction > 1) {            fraction = 1;        }        int alphaStart = Color.alpha(startColor);        int redStart = Color.red(startColor);        int blueStart = Color.blue(startColor);        int greenStart = Color.green(startColor);        int alphaEnd = Color.alpha(endColor);        int redEnd = Color.red(endColor);        int blueEnd = Color.blue(endColor);        int greenEnd = Color.green(endColor);        //计算差值        int alphaDifference = alphaEnd - alphaStart;        int redDifference = redEnd - redStart;        int greenDifference = greenEnd - greenStart;        int blueDifference = blueEnd - blueStart;        //根据进度计算当前色值        int alphaCurrent = alphaStart + (int) (fraction * alphaDifference);//[0..255]        int redCurrent = redStart + (int) (fraction * redDifference);//[0..255]        int blueCurrent = blueStart + (int) (fraction * blueDifference);//[0..255]        int greenCurrent = greenStart + (int) (fraction * greenDifference);//[0..255]        return Color.argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent);    }

附录:ArgbEvaluator类:

ARGB8888 四通道高精度(32位)

其中字母表示通道类型,数值表示该类型用多少位二进制来描述。

ARGB8888则表示有四个通道(ARGB),每个对应的通道均用8位来描述。

public class ArgbEvaluator implements TypeEvaluator {    private static final ArgbEvaluator sInstance = new ArgbEvaluator();    public static ArgbEvaluator getInstance() {        return sInstance;    }    /**     *  这个函数返回一个给定颜色值之间的数值,该值表示32位int的四个字节中的起始值和结束值。     *  每个通道分别线性插值,并将所得计算值重新组合为返回值。     * @param fraction 从起始值到结束值的进度     * @param startValue 起始颜色:一个32位int值,表示参数的独立字节中的颜色。     * @param endValue 结束颜色:一个32位int值,表示参数的独立字节中的颜色。     * @return 一个被计算为线性插值结果的值,     *         通过将起始值和结束值分离成单独的颜色通道并分别插入每个颜色通道而得到,      *         然后以相同的方式重新组合结果值。     */    public Object evaluate(float fraction, Object startValue, Object endValue) {        int startInt = (Integer) startValue;        int startA = (startInt >> 24) & 0xff;        int startR = (startInt >> 16) & 0xff;        int startG = (startInt >> 8) & 0xff;        int startB = startInt & 0xff;        int endInt = (Integer) endValue;        int endA = (endInt >> 24) & 0xff;        int endR = (endInt >> 16) & 0xff;        int endG = (endInt >> 8) & 0xff;        int endB = endInt & 0xff;        return (int)((startA + (int)(fraction * (endA - startA))) << 24) |                (int)((startR + (int)(fraction * (endR - startR))) << 16) |                (int)((startG + (int)(fraction * (endG - startG))) << 8) |                (int)((startB + (int)(fraction * (endB - startB))));    }}

这里写图片描述

原创粉丝点击