Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

drawable - Android: Change Shape Color in runtime

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.

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>

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

Any clue? I've passed the whole day googling... and it's getting pretty annoying...

UPDATE:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...