本文介绍了编程创建进度绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种情况,我需要有大量的进度条可绘制。我不能为所有的人创造XML的资源,因为我希望用户选择将被用于动态创建可绘制的颜色。下面就是这样的一个绘制在XML中,我怎么能编程方式创建这个确切绘制?
<层列表的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@机器人:ID /背景>
<形状>
[固体机器人:颜色=@色/透明/>
<行程机器人:宽=2px的机器人:颜色=@色/ category_blue_stroke/>
< /形状>
< /项目>
<项目机器人:ID =@机器人:ID /进步>
<夹>
<形状>
[固体机器人:颜色=@色/ category_blue/>
<行程机器人:宽=2px的机器人:颜色=@色/ category_blue_stroke/>
< /形状>
< /剪辑>
< /项目>
< /层列表>
解决方案
从由Rajesh和g00dy提供的链接,我能拿出一个解决方案。
公共静态绘制对象createDrawable(上下文的背景下){
ShapeDrawable形状=新ShapeDrawable();
。shape.getPaint()的setStyle(Style.FILL);
shape.getPaint()。setColor(
。context.getResources()的getColor(R.color.transparent));
。shape.getPaint()的setStyle(Style.STROKE);
shape.getPaint()setStrokeWidth(4)。
shape.getPaint()。setColor(
。context.getResources()的getColor(R.color.category_green_stroke));
ShapeDrawable形=新ShapeDrawable();
。shapeD.getPaint()的setStyle(Style.FILL);
shapeD.getPaint()。setColor(
。context.getResources()的getColor(R.color.category_green));
ClipDrawable clipDrawable =新ClipDrawable(形,Gravity.LEFT,
ClipDrawable.HORIZONTAL);
LayerDrawable layerDrawable =新LayerDrawable(新绘制对象[] {
clipDrawable,形状});
返回layerDrawable;
}
这code将创建一个可绘制的是视觉上类似于我的问题的XML创建。
I have a scenario where i need to have a large number of progress bar drawables. I cant create xml resources for all of them because i want the user to choose a color that will then be used to dynamically create the drawable. Below is one such drawable in xml, how can i create this exact drawable programmatically?
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="@color/transparent" />
<stroke android:width="2px" android:color="@color/category_blue_stroke"/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@color/category_blue" />
<stroke android:width="2px" android:color="@color/category_blue_stroke"/>
</shape>
</clip>
</item>
</layer-list>
解决方案
From the links provided by Rajesh and g00dy, i was able to come up with a solution.
public static Drawable createDrawable(Context context) {
ShapeDrawable shape = new ShapeDrawable();
shape.getPaint().setStyle(Style.FILL);
shape.getPaint().setColor(
context.getResources().getColor(R.color.transparent));
shape.getPaint().setStyle(Style.STROKE);
shape.getPaint().setStrokeWidth(4);
shape.getPaint().setColor(
context.getResources().getColor(R.color.category_green_stroke));
ShapeDrawable shapeD = new ShapeDrawable();
shapeD.getPaint().setStyle(Style.FILL);
shapeD.getPaint().setColor(
context.getResources().getColor(R.color.category_green));
ClipDrawable clipDrawable = new ClipDrawable(shapeD, Gravity.LEFT,
ClipDrawable.HORIZONTAL);
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {
clipDrawable, shape });
return layerDrawable;
}
This code will create a drawable that is visually similar to what the xml in my question creates.
这篇关于编程创建进度绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!