创建一个形状动态 [英] Create a Shape dynamically
本文介绍了创建一个形状动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在XML定义如下图所示的形状对象:
I have a shape object defined in XML like below:
<shape android:shape="rectangle">
<gradient
android:startColor="#333"
android:centerColor="#DDD"
android:endColor="#333"/>
<stroke android:width="1dp" android:color="#FF333333" />
</shape>
我想在我的code创建一个平等的对象。
我创建了一个 GradientDrawable
如下:
gradientDrawable1.setColors(new int[] { 0x333, 0xDDD, 0x333 });
gradientDrawable1.setOrientation(Orientation.TOP_BOTTOM);
但我不知道如何创建一个行程(?),然后分配笔触和 GradientDrawable
到图形
任何想法?
推荐答案
例如:
import android.graphics.drawable.GradientDrawable;
public class SomeDrawable extends GradientDrawable {
public SomeDrawable(int pStartColor, int pCenterColor, int pEndColor, int pStrokeWidth, int pStrokeColor, float cornerRadius) {
super(Orientation.BOTTOM_TOP,new int[]{pStartColor,pCenterColor,pEndColor});
setStroke(pStrokeWidth,pStrokeColor);
setShape(GradientDrawable.RECTANGLE);
setCornerRadius(cornerRadius);
}
}
用法:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SomeDrawable vDrawable = new SomeDrawable(Color.BLACK,Color.GREEN,Color.LTGRAY,2,Color.RED,50);
View vView = new View(this);
vView.setBackgroundDrawable(vDrawable);
setContentView(vView);
}
}
结果:
这篇关于创建一个形状动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文