【问题标题】:How to change color of drawable shapes in android如何在android中更改可绘制形状的颜色
【发布时间】:2013-02-28 04:11:41
【问题描述】:

我正在开发小型 android 应用程序,其中我将可绘制资源设置为线性布局的背景。现在我想要做的是动态更改线性布局的背景颜色,但在可绘制资源内。 我的代码看起来像:

//  bcd.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item>        
    <shape>
        <gradient
            android:endColor="#22000000"
            android:startColor="#22000000"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/white" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<LinearLayout 
android:id="@+id/lin_llt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

我在我的活动中为线性布局设置背景......

parentLayout = (LinearLayout) view.findViewById(R.id.lin_llt);
parentLayout.setBackgroundResource(R.drawable.bcd);

现在我想做的是改变我的可绘制资源的颜色,这意味着改变我的线性布局的颜色,圆角和填充在可绘制中定义..

我尝试了以下方式

ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground();
bgShape.getPaint().setColor(Color.BLACK);

但它不适合我。任何其他解决方案。

那该怎么做呢... 需要帮忙... 谢谢...

【问题讨论】:

标签: android background drawable android-linearlayout


【解决方案1】:

动态改变布局颜色

LinearLayout Layout = (LinearLayout) findViewById(R.layout.id);
Layout.setBackgroundColor(Color.parseColor("#ffffff"));

动态设置背景色渐变

View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

【讨论】:

  • 嗨,priya,谢谢您的回复。我可以以这种方式设置背景颜色,但之后我将无法保留那些以可绘制形状定义的圆角和填充,对吗?但我也想保留这些……还有其他解决方案吗……
  • 嗨 priya 谢谢你给我确切的解决方案。它适用于我的。它工作正常。你能用那个答案更新你的答案,以便其他人也可以使用它吗?
  • @APriya 你能帮我解决我的问题中的类似问题吗:stackoverflow.com/questions/35398658/…
【解决方案2】:

你可以试试这样的:

Drawable sampleDrawable = context.getResources().getDrawable(R.drawable.balloons); 
sampleDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

更多内容可以参考:

How to change colors of a Drawable in Android?

Change drawable color programmatically

Android: Change Shape Color in runtime

http://pastebin.com/Hd2aU4XC

你也可以试试这个:

private static final int[] FROM_COLOR = new int[]{49, 179, 110};
private static final int THRESHOLD = 3;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_colors);

    ImageView iv = (ImageView) findViewById(R.id.img);
    Drawable d = getResources().getDrawable(RES);
    iv.setImageDrawable(adjust(d));
}

private Drawable adjust(Drawable d)
{
    int to = Color.RED;

    //Need to copy to ensure that the bitmap is mutable.
    Bitmap src = ((BitmapDrawable) d).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y))) 
                bitmap.setPixel(x, y, to);

    return new BitmapDrawable(bitmap);
}

private boolean match(int pixel)
{
    //There may be a better way to match, but I wanted to do a comparison ignoring
    //transparency, so I couldn't just do a direct integer compare.
    return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD && Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD && Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}

How to change colors of a Drawable in Android?中所述

【讨论】:

  • 嗨 lokoko 谢谢你的回复。但是当我尝试你的解决方案时,它给了我空指针异常。我在做什么错了。需要帮助
  • 嗨 lokoko,谢谢你的回复。我只是问上面的解决方案是用于图像可绘制还是它也适用于可绘制形状。因为它将可绘制转换为位图。
  • 感谢 lokoko 的帮助。我尝试了您的第一个解决方案。我对我的代码做了一些更改。现在它的工作没有任何错误。唯一的问题是它没有应用改变的颜色。这意味着它可以正常运行但不会改变该drawable的颜色..
  • 我试过这样 Drawable sampleDrawable = context1.getResources().getDrawable(R.drawable.bcd); sampleDrawable.setColorFilter(new PorterDuffColorFilter(0xff0000,PorterDuff.Mode.MULTIPLY)); parentLayout.setBackgroundDrawable(sampleDrawable);
【解决方案3】:

以下方法非常适合以编程方式设置可绘制对象的颜色而不改变其形状:

parentLayout.getBackground().setColorFilter(
    Color.BLACK,
    PorterDuff.Mode.SRC_ATOP
);

【讨论】:

    【解决方案4】:

    使用这个..

    <solid android:color="#e1e1e1" />
    
    <stroke
        android:width="2dp"
        android:color="#808080" />
    
    <corners android:radius="10dp" />
    
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
    

    【讨论】:

      【解决方案5】:

      一种方法是使用第二个颜色创建第二个可绘制 XML,然后使用第二个可绘制对象更改布局的背景。

      【讨论】:

      • 嗨 androidguy 谢谢你的回复。我想动态改变那个drawable的颜色。所以不想创建不同的布局 xml 文件。有没有其他解决办法
      • 你好,尼尔卡什。我不知道,但我期待听到其他解决方案。
      • @AndroidGuy 你的解决方案是我希望找到的解决方案,因为我在多个地方重复使用drawable 并且xml drawable 上的setbackgroundcolor() 是@ 987654322@
      【解决方案6】:

      也可以使用:

      val layerDrawable : LayerDrawable = imageView.background as LayerDrawable
      val bgShape = layerDrawable.findDrawableByLayerId(R.id.shape_id) as GradientDrawable
      bgShape.setColor(ContextCompat.getColor(context, R.color.colorPrimary))   
      

      举例(drawable/circle.xml):

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:id="@+id/shape_id">
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
              android:shape="rectangle">
              <corners android:radius="1000dp" />
              <solid android:color="#F4B528" />
              <padding
                  android:bottom="4dp"
                  android:left="4dp"
                  android:right="4dp"
                  android:top="4dp" />
          </shape>
      </item>
      </layer-list>
      

      ImageView

      <ImageView
          android:id="@+id/imageView"
          android:layout_width="16dp"
          android:layout_height="16dp"
          android:background="@drawable/circle" 
      />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        • 2017-12-01
        相关资源
        最近更新 更多