Android:在运行时更改形状颜色

时间:2011-05-09 18:20:26

标签: android drawable

我有一个drawable,我用它作为LinearLayout的背景。我想在运行时更改此Shape的颜色。我尝试过使用多种方法..但都没有用。

我遵循了此处描述的方法: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();

有任何线索吗?我已经过了一整天的谷歌搜索...而且它变得很烦人......

更新

当我尝试对此形状做同样的事情时:

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

颜色变成黑色...我想它可以改变......

8 个答案:

答案 0 :(得分:41)

我现在正在创建一个类似于预编译器的Drawable ..因为我无法将颜色更改为除黑色之外的任何颜色,即使在尝试下面描述的十六进制或之后也是如此。

新代码:

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

无论如何这是一个修复..第一个问题的解决方案是我真正想要的!当然,我会感激任何帮助!

答案 1 :(得分:34)

看看类似的东西是否适合你:

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

在我的情况下,我有一个TextDrawable作为背景。我想改变它的颜色,并设法使这项工作。令人费解的是,tv2.getBackground()返回一个GradientDrawable而不是一个ShapeDrawable - 这在其他地方也有报道。

编辑:关于颜色,请尝试将Alpha值设置为0xff。如果您注意到,即使在上面的代码中,setColor()函数也会从常规RGB十六进制值中获取额外的十六进制值。这适用于Alpha / Opacity。如果将其设置为0x00,Drawable将具有黑色,与RGB无关(假设您的背景颜色为黑色)。 0x00是一个完全透明的对象&amp; 0xff是一个完全不透明的对象。

答案 2 :(得分:28)

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

setColor方法正确地请求对元素进行重绘 (如果在xml中使用了&lt; shape&gt;元素,它将始终成为GradientDrawable)

答案 3 :(得分:5)

有一种更简单的方法:

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

答案 4 :(得分:5)

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

答案 5 :(得分: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)

然后设置该颜色。

答案 6 :(得分:-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));

答案 7 :(得分:-1)

使用十六进制代码/值设置GradientDrawable形状颜色
只需将0x添加到十六进制颜色值。

    GradientDrawable shape =  new GradientDrawable();
    shape.setCornerRadius( 16 );
    shape.setColor(0xff33b5e5);
    ButtonStartSurvey.setBackground(shape);
相关问题