Android的:在运行时改变形状颜色 [英] Android: Change Shape Color in runtime
问题描述
我有我作为背景的LinearLayout中使用的可绘制。我想改变这种形态在运行时的颜色。我已经使用几种方法试过..但没有工作。
I have a drawable that i use as a Background for a LinearLayout. I would like to change the color of this Shape in runtime. I have tried using several methods.. but none work.
我已经按照这里介绍的方法:<一href="http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/change-shape-drawable-solid-color-t16798.html">http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/change-shape-drawable-solid-color-t16798.html
I've followed the approach described here: http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/change-shape-drawable-solid-color-t16798.html
但有同样的问题......它不崩溃..但颜色不改变!
But have the same problem... it doesnt crashes.. but the color doesnt change!
<?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>
的code
摘录:
Snippet of code:
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过去了......而且越来越pretty的讨厌...
Any clue? I've passed the whole day googling... and it's getting pretty annoying...
更新:
当我尝试做同样的这个形状:
When i try to do the same to this 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>
颜色变为黑色...我想告诉它是可以改变的......
The color turns to black... what i guess tells it can be changed...
推荐答案
我现在创建像一个pre-编译器..因为我无法改变颜色来什么,但黑色的,即使在绘制对象尝试十六进制或描述如下。
I'm now creating a Drawable like the one pre-compiler.. as i couldn't change the color to anything but black, even after trying the hex OR described below.
新的code:
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);
总之,这是一个修复。对于第一个问题的解决方案就是我真正需要的!我将AP preciate当然任何帮助!
Anyway this is a fix.. a solution for the first question is what i'm really looking for! I'll appreciate any help of course!
这篇关于Android的:在运行时改变形状颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!