美文网首页
android 动态修改view的shape颜色(渐变色)

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

作者: hao_developer | 来源:发表于2020-11-26 12:02 被阅读0次

    效果图修改前:


    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);
    

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

    相关文章

      网友评论

          本文标题:android 动态修改view的shape颜色(渐变色)

          本文链接:https://www.haomeiwen.com/subject/lwpaiktx.html