【问题标题】:Android: Change Shape Color in runtimeAndroid:在运行时更改形状颜色
【发布时间】:2011-08-21 21:36:55
【问题描述】:

我有一个可绘制对象,用作线性布局的背景。我想在运行时更改此形状的颜色。我尝试了几种方法..但没有一个工作。

我遵循了这里描述的方法: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>

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

【问题讨论】:

  • 只是猜测。 Drawable 是不可改变的,所以你需要创建一个副本,然后改变那个副本。
  • 为什么在第二个例子中它变成黑色?! :-S
  • 你设置成什么颜色?黑色?
  • 没有。黄色的。我注意到的一件事是我传递的颜色是 RGB,它需要 ARGB。我使用的颜色是对字符串#RRGGBB 的解析。
  • 在这种情况下,请使用已解析数字和 0xFF000000 的二进制 OR。

标签: android drawable


【解决方案1】:

我现在正在创建一个类似于预编译器的 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);

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

【讨论】:

  • 我发现使用 Shape Drawable 技术生成圆角比使用 Gradient Drawable 圆角方法更好。 Gradient Drawable 似乎存在一个错误,即当应用程序启动并且屏幕抖动一次时,某些角不是圆角(导致按钮出现剪裁)。 Shape Drawable 圆角对我来说 100% 有效。
【解决方案2】:

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

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

在我的例子中,我有一个 TextView,它有一个 ShapeDrawable 作为背景。我想改变它的颜色并设法完成这项工作。莫名其妙的是,tv2.getBackground() 返回的是 GradientDrawable 而不是 ShapeDrawable——这在其他地方也有报道。

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

【讨论】:

  • @NeTeInStEiN 看看我刚刚添加的黑色的解释。如果这完全解决了您的问题,我会要求您选择此作为正确答案。
  • +1 @SaurabhNanda 解决了我的问题。很奇怪,它是 GradientDrawable 不是吗!
  • @SaurabhNanda 你能帮我解决类似的问题吗:stackoverflow.com/questions/35398658/…
  • 在我看来,这是任何答案的关键部分。就我而言,invalidateSelf() 至关重要。
【解决方案3】:
GradientDrawable background = (GradientDrawable) titleTextView.getBackground();
background.setColor(getResources().getColor(R.color.some_color));

setColor 方法正确地请求重绘元素 (如果在 xml 中你使用了 元素,它总是会变成 GradientDrawable)

【讨论】:

  • 在将其颜色更改为正确的颜色之前,请确保不要在其他任何地方重复使用此可绘制对象。在 API
  • 感谢兄弟,我为这个愚蠢的事情想了将近 2 个小时
【解决方案4】:

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

【讨论】:

  • 对不起 colorToPaint = getResources().getColor()
【解决方案5】:

有一个更简单的方法:

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

【讨论】:

  • 您的回答与问题无关……请仔细阅读。
  • @NeTeInStEiN:这与问题有关。您应该在下次关闭它之前尝试一下。
  • @kospol 你可以更好地解释你的答案
【解决方案6】:

这就是我在动态壁纸中所做的,我在运行时修改了 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)

然后设置该颜色。

【讨论】:

  • 原件和复印件分别是什么类型?
  • 即使有它..它只会变黑:-(
  • 叹息,在这种情况下我需要调试它,但不幸的是,这不是我想做的事情:(
【解决方案7】:

我目前的解决方法是使用不带颜色选项的可绘制对象。我把它放在一个框架布局里面,然后动态设置框架布局对象的背景颜色。从技术上讲,它仍然是一个“修复”,但在我看来这是最简单的选择。

布局文件:

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

【讨论】:

  • 对不起,伙计们。没有意识到你不能塑造你的形状(即我的形状有圆角,但背景颜色填充了整个空间,所以你看不出来)。此修复仅适用于适合框架的形状,不适用于圆形或带圆角的矩形。
【解决方案8】:

使用十六进制代码/值设置 GradientDrawable Shape 颜色
只需将 0x 前缀为十六进制颜色值。

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

【讨论】:

    猜你喜欢
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 2020-05-14
    相关资源
    最近更新 更多