I have a drawable from an XML resource, and I want to use that drawable but set the gradient color dynamically. So far I have something like this:
我有一个XML资源的drawable,我想使用drawable但动态设置渐变颜色。到目前为止,我有这样的事情:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="3dip">
</corners>
<gradient
android:angle="90"
android:type="linear"
android:startColor="#FFFFFFFF"
android:centerColor="#FFFF0000"
android:endColor="#FFFF0000">
</gradient>
</shape>
Now I figured that I would be able to make the colors dynamically by getting the drawable at runtime, casting it as a GradientDrawable, and using a method to set the colors. The GradientDrawable however does not have such a method, and one can only set the colors in the constructor. I find it very strange that this is the case because all the other aspects of the gradient are settable. Is there an easier way than overriding onDraw() and doing the gradient myself? Some of the classes I'm trying to use are very poorly documented..
现在我想通过在运行时获取drawable,将其转换为GradientDrawable并使用方法设置颜色,我可以动态制作颜色。但是,GradientDrawable没有这样的方法,只能在构造函数中设置颜色。我觉得这很奇怪,因为渐变的所有其他方面都是可设置的。有没有比覆盖onDraw()和自己做渐变更简单的方法?我试图使用的一些课程记录很差。
3 个解决方案
#1
0
you can set as background drawable for a view dynamically.
您可以动态地为视图设置为可绘制的背景。
view.setBackgroundDrawable(R.drawable.your_drawable_id);
view.setBackgroundDrawable(R.drawable.your_drawable_id);
#2
0
Resources are primarily static, and typically do not allow modification. Some resource types allow you to "clone" a mutable copy. GradientDrawable
only allows you to set the colors in the contstuctor (as you discovered), so you need to create those internally if you want to control the colors dynamically at runtime, or better yet, select one of a fixed number of backgrounds from resource instead. As mentioned previously, use setBackgroundDrawable()
to install your background at runtime. No need to pass judgment, just Get-R-Done!
资源主要是静态的,通常不允许修改。某些资源类型允许您“克隆”可变副本。 GradientDrawable只允许您在contstuctor中设置颜色(如您所发现的),因此如果要在运行时动态控制颜色,则需要在内部创建颜色,或者更好的是,从资源中选择固定数量的背景之一。如前所述,使用setBackgroundDrawable()在运行时安装后台。没必要通过判断,只需Get-R-Done!
#3
0
Make a GradientDrawable Class like this:
像这样创建一个GradientDrawable类:
public class RoundedDrawable extends GradientDrawable {
public RoundedDrawable(int shape, int solidColor, int strokeWidth,
int strokeColor, float[] fourRadii) {
this.mutate();
this.setShape(shape);
this.setColor(solidColor);
this.setStroke(strokeWidth, strokeColor);
this.setCornerRadii(fourRadii);
}
}
Now use this in your Activity like this:
现在在你的Activity中使用它,如下所示:
public class AAActivity extends Activity {
公共类AAActivity扩展Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_transaction_layout);
RoundedDrawable customBg;
RelativeLayout relList = (RelativeLayout) findViewById(R.id.relList);
float radii[]={5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f};
customBg = new RoundedDrawable(GradientDrawable.RECTANGLE,Color.parseColor("#FFFFFF"),
2, Color.parseColor("#8C8C8C"),radii);
relList.setBackgroundDrawable(customBg);
LinearLayout linearItemsRow = (LinearLayout) findViewById(R.id.linearItemsRow);
float[] rowRadii={5.0f, 5.0f, 5.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f};
customBg = new RoundedDrawable(GradientDrawable.RECTANGLE,Color.parseColor("#CBCBCB"),
0, 0, rowRadii);
linearItemsRow.setBackgroundDrawable(customBg);
}
}
}
Hope this will help.
希望这会有所帮助。
#1
0
you can set as background drawable for a view dynamically.
您可以动态地为视图设置为可绘制的背景。
view.setBackgroundDrawable(R.drawable.your_drawable_id);
view.setBackgroundDrawable(R.drawable.your_drawable_id);
#2
0
Resources are primarily static, and typically do not allow modification. Some resource types allow you to "clone" a mutable copy. GradientDrawable
only allows you to set the colors in the contstuctor (as you discovered), so you need to create those internally if you want to control the colors dynamically at runtime, or better yet, select one of a fixed number of backgrounds from resource instead. As mentioned previously, use setBackgroundDrawable()
to install your background at runtime. No need to pass judgment, just Get-R-Done!
资源主要是静态的,通常不允许修改。某些资源类型允许您“克隆”可变副本。 GradientDrawable只允许您在contstuctor中设置颜色(如您所发现的),因此如果要在运行时动态控制颜色,则需要在内部创建颜色,或者更好的是,从资源中选择固定数量的背景之一。如前所述,使用setBackgroundDrawable()在运行时安装后台。没必要通过判断,只需Get-R-Done!
#3
0
Make a GradientDrawable Class like this:
像这样创建一个GradientDrawable类:
public class RoundedDrawable extends GradientDrawable {
public RoundedDrawable(int shape, int solidColor, int strokeWidth,
int strokeColor, float[] fourRadii) {
this.mutate();
this.setShape(shape);
this.setColor(solidColor);
this.setStroke(strokeWidth, strokeColor);
this.setCornerRadii(fourRadii);
}
}
Now use this in your Activity like this:
现在在你的Activity中使用它,如下所示:
public class AAActivity extends Activity {
公共类AAActivity扩展Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_transaction_layout);
RoundedDrawable customBg;
RelativeLayout relList = (RelativeLayout) findViewById(R.id.relList);
float radii[]={5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f};
customBg = new RoundedDrawable(GradientDrawable.RECTANGLE,Color.parseColor("#FFFFFF"),
2, Color.parseColor("#8C8C8C"),radii);
relList.setBackgroundDrawable(customBg);
LinearLayout linearItemsRow = (LinearLayout) findViewById(R.id.linearItemsRow);
float[] rowRadii={5.0f, 5.0f, 5.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f};
customBg = new RoundedDrawable(GradientDrawable.RECTANGLE,Color.parseColor("#CBCBCB"),
0, 0, rowRadii);
linearItemsRow.setBackgroundDrawable(customBg);
}
}
}
Hope this will help.
希望这会有所帮助。