Android:在运行时更改形状颜色
我有一个可绘制的,我用它作为线性布局的背景。我想在运行时更改此形状的颜色。我试过几种方法。。但都不管用 我遵循了这里描述的方法: 但是有同样的问题。。。它不会崩溃。。但是颜色没有改变Android:在运行时更改形状颜色,android,drawable,Android,Drawable,我有一个可绘制的,我用它作为线性布局的背景。我想在运行时更改此形状的颜色。我试过几种方法。。但都不管用 我遵循了这里描述的方法: 但是有同样的问题。。。它不会崩溃。。但是颜色没有改变 <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#00A6C1" /&
<?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>
有线索吗?我在谷歌上搜索了一整天。。。而且它变得很烦人
更新:
当我尝试对该形状执行相同操作时:
<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>
颜色变成黑色。。。我猜它是可以更改的…这就是我在实时墙纸中所做的,在运行时修改可绘制的:
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)
然后设置颜色。我现在正在创建一个类似预编译器的可绘制文件。。因为我无法将颜色更改为除黑色以外的任何颜色,即使在尝试了十六进制或下面描述的颜色之后 新守则:
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);
无论如何,这是一个解决办法。。第一个问题的解决方案是我真正想要的!当然,我会感谢任何帮助 看看类似的东西是否适合你:
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();
在我的例子中,我有一个文本视图,它有一个可变形的背景。我想改变它的颜色,并设法使这项工作。令人费解的是,tv2.getBackground()返回的是GradientDrawable而不是ShapeDrawable——这在其他地方也有报道
编辑:关于颜色,请尝试将alpha值设置为0xff。如果您注意到,即使在我上面的代码中,setColor()函数也会在常规RGB十六进制值之外获取一个额外的十六进制值。这是针对Alpha/不透明度的。如果设置为0x00,则无论RGB如何(假设背景色为黑色),可绘制图形都将为黑色。0x00是一个完全透明的对象&0xff是一个完全不透明的对象
GradientDrawable background = (GradientDrawable) titleTextView.getBackground();
background.setColor(getResources().getColor(R.color.some_color));
setColor方法正确地请求对元素进行重画
(如果在xml中使用元素,它将始终成为GradientDrawable)有一种更简单的方法:
ShapeDrawable drawable = new ShapeDrawable();
drawable.getPaint().setColor(getResources().getColor(R.color.blue));
getActionBar().setBackgroundDrawable(drawable);
我目前的解决方案是使用一个不带颜色选项的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>
R.drawable.library\u 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>
使用十六进制代码/值设置GradientDrawable形状颜色
只需将0x作为十六进制颜色值的前缀即可
GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius( 16 );
shape.setColor(0xff33b5e5);
ButtonStartSurvey.setBackground(shape);
只是猜测而已。Drawable是不可更改的,所以您需要创建一个副本,并对其进行更改。为什么在第二个示例中它会变为黑色-你把它设置成什么颜色?黑色?不,黄色。我注意到的一点是,我传递的颜色是RGB,它需要ARGB。我使用的颜色是解析字符串#RRGGBB。在这种情况下,使用二进制还是解析后的数字和0xFF000000。什么类型是原始类型和副本?即使使用它。。它只会变成黑色:-(叹气,在这种情况下我需要调试它,但不幸的是,这不是我想做的事情:(@NeTeInStEiN看一看我刚才添加的黑色解释。如果这完全解决了你的问题,我会要求你选择这个作为正确答案。+1@SaurabhNanda解决了我的问题。非常奇怪,这是一个渐变绘图,不是吗!@SaurabhNanda你能帮我解决一些类似的问题吗:在我看来,这是一个cr答案的关键部分。在我的例子中,无效自我()是至关重要的。你的答案与问题无关……请仔细阅读。@NeTeInStEiN:它与问题有关。你应该在下次驳回它之前尝试一下。@kospol你可以更好地解释你的答案对不起,伙计们。没有意识到你不能塑造自己的形状(即,我的形状有圆角,但背景色填充整个空间,因此您无法分辨)。此修复程序仅适用于适合框架的形状,而不适用于圆形或圆角矩形。请确保在将其颜色更改为正确颜色之前,不要在其他任何地方重复使用此可绘制图形。在ApiThank brother上,我想了近2个小时才知道这一愚蠢的东西SSORRY colorToPaint=getResources().getColor()我发现使用形状可绘制技术生成圆角比使用渐变可绘制圆角方法更好。渐变可绘制方法似乎存在一个缺陷,其中一些角不圆角(导致按钮看起来被剪裁)当应用程序启动时,屏幕会抖动一次。形状可绘制的圆角对我来说100%有效。
mHolder.dateLayout.setBackgroundColor(getResources().getColor(R.color.SecondaryGreen));
<?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);
GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius( 16 );
shape.setColor(0xff33b5e5);
ButtonStartSurvey.setBackground(shape);