android 动态修改view的shape颜色(渐变色)

效果图修改前:


image.png

效果图修改后:


image.png

orange_shape文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient android:angle="0"
        android:startColor="@android:color/holo_red_dark"
        android:centerColor="@android:color/holo_green_dark"
        android:endColor="@android:color/holo_blue_light"/>

</shape>

布局layout

 <View
        android:id="@+id/topView"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/orange_shape" />

activity(这种方案必须有设置shape后才能使用)

int[] colorsTwo = {Color.parseColor("#FFA538"),Color.parseColor("#FFC848"),Color.parseColor("#FFC848"),};
GradientDrawable backView = (GradientDrawable) view.getBackground();
backView.setColors(colorsTwo);

另外一种方案

int[] colors = {0xFFFF9326,0xFFFFC54E};
int[] colors = {Color.parseColor("#ffffff"),Color.parseColor("#ffffff")};
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,colors);
drawable.setCornerRadius(0);
view.setBackground(drawable);

有更好的实现方案,希望小伙伴给与文章参考连接或思路

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容