У меня есть требование в моем проекте динамически рисовать круг во время выполнения. Поэтому для этой цели я использую 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");
}
xml для TextView txtCount
в моем проекте для is
<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. Свойство по-прежнему ведет себя как овал.
width == height
, то овал должен быть кругом? - person Blackbelt   schedule 22.07.2015