Android颜色渐变效果

本文详细介绍了如何使用Android中Color类的方法实现颜色渐变效果,通过一个具体的Demo展示了颜色从红色到绿色的平滑过渡过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在看TextinputLayout源码的时候,里面有个类叫做CollapsingTextHelper。其中有段颜色渐变效果的代码,现在贴出来记录一下。

 /**
     * 颜色渐变
     *
     * @param color1 起始颜色
     * @param color2 终止颜色
     * @param ratio 颜色变化频率 从0-1
     * @return 颜色值
     */
    private static int blendColors(int color1, int color2, float ratio) {
        float inverseRatio = 1.0F - ratio;
        float a = (float) Color.alpha(color1) * inverseRatio + (float) Color.alpha(color2) * ratio;
        float r = (float) Color.red(color1) * inverseRatio + (float) Color.red(color2) * ratio;
        float g = (float) Color.green(color1) * inverseRatio + (float) Color.green(color2) * ratio;
        float b = (float) Color.blue(color1) * inverseRatio + (float) Color.blue(color2) * ratio;
        return Color.argb((int) a, (int) r, (int) g, (int) b);
    }

 

 

配合以下Demo代码就可以看出效果:

public class ColorActivity extends AppCompatActivity {
    private View view;
    int color1 = 0xffff0000;
    int color2 = 0xff00ff00;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int a = msg.arg1;

            if (a <= 1000) {
                Message message = obtainMessage();
                message.arg1 = a + 1;
                sendMessageDelayed(message,1);
                float fraction = a/1000f;
                Log.d("wsw",fraction+"==");
                int color = blendColors(color1,color2,fraction);
                view.setBackgroundColor(color);
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        view = findViewById(R.id.view);
    }

    public void start(View view) {
        Message msg = new Message();
        msg.arg1 = 0;
        mHandler.sendMessageDelayed(msg,1);
            TextInputLayout
    }

    /**
     * 颜色渐变
     *
     * @param color1 起始颜色
     * @param color2 终止颜色
     * @param ratio 颜色变化频率 从0-1
     * @return 颜色值
     */
    private static int blendColors(int color1, int color2, float ratio) {
        float inverseRatio = 1.0F - ratio;
        float a = (float) Color.alpha(color1) * inverseRatio + (float) Color.alpha(color2) * ratio;
        float r = (float) Color.red(color1) * inverseRatio + (float) Color.red(color2) * ratio;
        float g = (float) Color.green(color1) * inverseRatio + (float) Color.green(color2) * ratio;
        float b = (float) Color.blue(color1) * inverseRatio + (float) Color.blue(color2) * ratio;
        return Color.argb((int) a, (int) r, (int) g, (int) b);
    }
}

 

效果如下图所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值