我正在尝试以编程方式创建一个ShapeDrawable,但以下代码没有显示任何内容。
ImageView image = new ImageView (context);
image.setLayoutParams (new LayoutParams (200, 200));
ShapeDrawable badge = new ShapeDrawable (new OvalShape());
badge.setBounds (0, 0, 200, 200);
badge.getPaint().setColor(Color.RED);
ImageView image = new ImageView (context);
image.setImageDrawable (badge);
addView (image);
我可以使用xml。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="200px"
android:height="200px" />
<solid
android:color="#F00" />
</shape>
ImageView image = new ImageView (context);
image.setLayoutParams (new LayoutParams (200, 200));
image.setImageResource (R.drawable.badge);
addView (image);
但我想以编程方式创建它。 xml完美运行所以问题不能与ImageView一起,它必须在创建ShapeDrawable。
答案 0 :(得分:11)
使用/home/user/project/INSTALLPATH/lib/
和setIntrinsicWidth
代替setIntrinsicHeight
来设置宽度和高度。
setBounds
答案 1 :(得分:0)
您可能需要创建一个扩展ShapeDrawable的类来覆盖onDraw
,然后创建一个类的实例。
示例:(Source - 查看完整示例的链接)
private static class MyShapeDrawable extends ShapeDrawable {
private Paint mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public MyShapeDrawable(Shape s) {
super(s);
mStrokePaint.setStyle(Paint.Style.STROKE);
}
public Paint getStrokePaint() {
return mStrokePaint;
}
@Override protected void onDraw(Shape s, Canvas c, Paint p) {
s.draw(c, p);
s.draw(c, mStrokePaint);
}
}