2011-05-09 54 views
46

我有一个drawable,我用作LinearLayout的背景。我想在运行时改变这个Shape的颜色。我曾尝试使用几种方法..但没有工作。Android:在运行时更改形状颜色

我已经按照这里介绍的方法:http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/change-shape-drawable-solid-color-t16798.html

但有同样的问题......它不崩溃..但颜色并没有改变!

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#00A6C1" /> 
    <corners android:radius="@dimen/square_corners" /> 
</shape> 

代码片段:

GradientDrawable drawable = (GradientDrawable) activity.getResources().getDrawable(R.drawable.blue_square_shape); 


int color = ((Application) getApplication()).getColor(); 
drawable.setColor(color); 

block.findViewById(R.id.blockSquare).setBackgroundDrawable(drawable); 

findViewById(R.id.blockSquare).postInvalidate(); 

任何线索?我已经通过了一整天google搜索......而且越来越烦...

UPDATE:

当我尝试做同样的这个形状:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/shape" android:shape="rectangle"> 
    <gradient android:startColor="#1FBCCF" android:endColor="#06A4C1" 
     android:angle="270" /> 
    <corners android:topLeftRadius="@dimen/footer_corners" 
     android:topRightRadius="@dimen/footer_corners" /> 
</shape> 

的颜色变成黑色...我猜它可以改变...

+0

只是一个猜测。 Drawable不可变,所以你需要创建一个副本,并对其进行变异。 – Kaj 2011-05-09 18:24:17

+0

为什么在第二个例子中变成黑色? :-S – neteinstein 2011-05-09 18:25:08

+0

你将它设置为什么颜色?黑色? – Kaj 2011-05-09 18:51:12

回答

39

我现在正在创建一个像一个预编译器的Drawable ..因为我无法将颜色更改为除黑色之外的任何东西,即使在尝试了下面描述的十六进制OR之后。

新代码:

ShapeDrawable footerBackground = new ShapeDrawable(); 

// The corners are ordered top-left, top-right, bottom-right, 
// bottom-left. For each corner, the array contains 2 values, [X_radius, 
// Y_radius] 
float[] radii = new float[8]; 
radii[0] = activity.getResources().getDimension(R.dimen.footer_corners); 
radii[1] = activity.getResources().getDimension(R.dimen.footer_corners); 

radii[2] = activity.getResources().getDimension(R.dimen.footer_corners); 
radii[3] = activity.getResources().getDimension(R.dimen.footer_corners); 

footerBackground.setShape(new RoundRectShape(radii, null, null)); 

int color = ((Application) activity.getApplication()).getColor(); 

footerBackground.getPaint().setColor(color); 

views.setBackgroundDrawable(footerBackground); 

反正这是一个修复。对于第一个问题的解决方案是什么我真的寻找!当然我会很感激任何帮助!

+0

我发现使用形状可绘制技术来产生圆角是比使用渐变可绘制圆角方法更好的解决方案,似乎有一个渐变可绘制的问题,即当应用程序启动并且屏幕抖动一次时,某些角落不会变圆(导致按钮出现剪切)形状可绘制圆角我为我工作100% – Zhang 2015-06-10 10:52:17

+0

谢谢你节省了我的时间+1票为你 – 2017-11-07 07:15:42

1

这就是我在一个动态壁纸,我在运行时修改Drawable:

this.original = DrawableFactory.getDrawable(getContext().getResources(), objectName)[0]; 
originalBitmap = original.getBitmap(); 
copy = new BitmapDrawable(getContext().getResources(), original.getBitmap().copy(Bitmap.Config.ARGB_8888, true)); 
copyCanvas = new Canvas(copy.getBitmap()); 

编辑:类型声明:

public Bitmap originalBitmap; 
public BitmapDrawable original; 
public BitmapDrawable copy; 
public Canvas copyCanvas; 

编辑2:

在这种情况下,试试这个:

int color = (0xFF000000 | yourParsedColor) 

然后设置颜色。

+0

什么类型是原创和复制? – neteinstein 2011-05-09 18:35:55

+0

即使有它..它只会变黑:-( – neteinstein 2011-05-10 11:01:19

+0

感叹,我需要在这种情况下进行调试,但不幸的是这不是我想做的事情:( – Kaj 2011-05-10 19:37:19

31

看看一些类似的对你的作品:

TextView tv2 = (TextView) rl.findViewById(R.id.toggle_indicator); 
/* Refer to http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#mutate() 
to understand why we need to mutate the GradientDrawable*/ 
GradientDrawable sd = (GradientDrawable) tv2.getBackground().mutate(); 
sd.setColor(0xff999999); 
sd.invalidateSelf(); 

在我来说,我有具有ShapeDrawable作为背景一个TextView。我想改变它的颜色,并设法做到这一点。令人费解的是,tv2.getBackground()返回一个GradientDrawable而不是一个ShapeDrawable - 这在其他地方也有报道。

编辑:关于颜色,请尝试设置一个alpha值为0xff。如果你注意到,即使在我上面的代码中,setColor()函数除了普通的RGB十六进制值以外还有一个额外的十六进制值。这是针对Alpha /不透明度的。如果设置为0x00,则Drawable将呈现黑色,而不考虑RGB(假设您的背景颜色为黑色)。 0x00是一个完全透明的对象& 0xff是一个完全不透明的对象。

+0

@NeTeInStEiN看看我刚刚添加的黑色的解释,如果这完全解决了你的问题,我会要求你选择这个作为正确的答案。 – 2011-12-16 11:14:43

+0

+1 @SaurabhNanda为了解决我的问题。非常奇怪的是它是一个GradientDrawable不是它! – 2012-08-24 01:40:02

+0

谢谢@saurab h nanda ..你解决了我的问题 – 2015-01-16 15:11:33

26
GradientDrawable background = (GradientDrawable) titleTextView.getBackground(); 
background.setColor(getResources().getColor(R.color.some_color)); 

的的setColor方法正确请求元素 上重绘(如果XML你使用的<形状>元素,它将永远成为GradientDrawable)

+1

请确保你没有在将其颜色更改为正确的颜色之前,在其他任何地方重新使用此可绘制对象。在API <21上,drawable保持最新的颜色集。 – 2015-04-22 17:33:52

+0

优秀的,我想要的是这个,谢谢 – 2016-12-28 07:22:34

4

还有一个更简单的方法:

ShapeDrawable drawable = new ShapeDrawable(); 
drawable.getPaint().setColor(getResources().getColor(R.color.blue)); 
getActionBar().setBackgroundDrawable(drawable); 
+1

你的答案与问题无关......阅读更仔细。 – neteinstein 2014-11-13 10:26:00

+3

@NenetInStEiN:这与问题有关。你应该在下次解雇之前尝试一下。 – 2016-04-06 19:12:22

+0

@kospol你可以更好地解释你的答案 – 2018-02-28 06:50:22

-1

我目前的修复涉及没有颜色选项的drawable。我把它放在一个框架布局中,然后动态地设置框架布局对象的背景颜色。这在技术上仍然是'修复',但在我看来这是最简单的选择。

布局文件:

<FrameLayout 
    android:id="@+id/dateLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@color/SecondaryGreen"> 

    <ImageView 
     android:id="@+id/dateBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/date_rectangle" /> 

</FrameLayout> 

日期矩形绘制对象文件:

<shape xmlns:android="http://schemas.android.com/apk/res/android" ><size android:width="50dp" android:height="50dp"/><corners android:radius="5dp"/></shape> 

动态渲染:

mHolder.dateLayout.setBackgroundColor(getResources().getColor(R.color.SecondaryGreen)); 
+0

对不起。没有意识到你不能塑造你的形状(即我的形状有圆角,但背景颜色填充整个空间,所以你不知道)。此修补程序仅适用于适合框架的形状,而不适用于圆角或带圆角的矩形。 – 2015-03-26 16:02:15

1

R.drawable.library_cirecle

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:id="@+id/outerRectangle"> 
    <shape android:shape="oval" > 
     <solid android:color="#FCD366" /> 

     <stroke 
      android:width="1dp" 
      android:color="@android:color/darker_gray" /> 
    </shape> 
</item> 
在代码

更改颜色

Drawable tempDrawable = getResources().getDrawable(R.drawable.library_cirecle); 
LayerDrawable bubble = (LayerDrawable) tempDrawable; (cast to root element in xml) 
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle); 
solidColor.setColor(colorToPaint); 
imageView.setImageDrawable(tempDrawable); 
+0

抱歉,colorToPaint = getResources()。getColor() – 2015-05-12 09:03:48

-1

使用十六进制代码/值
只需0x前缀,以十六进制颜色值设置GradientDrawable形状颜色。

GradientDrawable shape = new GradientDrawable(); 
    shape.setCornerRadius(16); 
    shape.setColor(0xff33b5e5); 
    ButtonStartSurvey.setBackground(shape);