我的项目要求在运行时动态绘制一个圆圈。因此,为此目的,我使用 ShapeDrawable 以编程方式创建圆,但不幸的是,我在 ShapeDrawable 中找不到用于 CircleShape 的任何类或方法,而我只找到了OvalShape()
. 因此,请帮助我通过 ShapeDrawable 仅通过圆的直径或半径来绘制圆。提前致谢。任何类型的定制都对我修复我的解决方案很有用。
我用于 ShapeDrawable 的代码是
public static ShapeDrawable drawCircle (Context context, int width, int height, int color) {
//////Drawing oval & Circle programmatically /////////////
ShapeDrawable oval = new ShapeDrawable (new OvalShape ());
oval.setIntrinsicHeight (height);
oval.setIntrinsicWidth (width);
oval.getPaint ().setColor (color);
return oval;
}
使用的代码MainActivity.java
if(Build.VERSION.SDK_INT >= 16) {
txtCount.setBackground (Util.drawCircle (MainActivity.this, 50, 50, getResources ().getColor (R.color.yellow)));
txtHotelCount.setText ("20");
}else{
txtCount.setBackgroundDrawable (Util.drawCircle (MainActivity.this, 50, 50, getResources ().getColor (R.color.yellow)));
txtHotelCount.setText ("20");
}
在我的项目中用于TextView 的xml是txtCount
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@color/white">
<TextView
android:id="@+id/txt_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_grey"
android:gravity="center"
android:textSize="12sp"
android:padding="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_grey"
android:text="AVAILABLE"
android:layout_marginLeft="10dp"
android:gravity="center"
/>
</LinearLayout>
但即使将宽度和高度设置为 50 后仍然没有运气。该属性的行为仍然像椭圆形。