2011-05-09 38 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);