Tôi có một yêu cầu trong dự án của mình để vẽ một vòng tròn trong thời gian chạy động. Vì vậy, với mục đích đó, tôi đang sử dụng ShapeDrawable để tạo vòng tròn theo lập trình, nhưng tiếc là tôi không thể tìm thấy bất kỳ lớp hoặc phương thức nào bên trong ShapeDrawable cho CircleShape, thay vào đó tôi chỉ tìm thấy OvalShape()
. Vì vậy, xin vui lòng giúp tôi vẽ một vòng tròn thông qua ShapeDrawable bằng cách chỉ cần đi qua đường kính hoặc bán kính của vòng tròn. Cảm ơn trước. Mọi loại tùy chỉnh sẽ hữu ích cho tôi để khắc phục giải pháp của tôi.Vẽ vòng tròn lập trình bằng cách sử dụng Android ShapeDrawable
Mã Tôi đang sử dụng cho ShapeDrawable là
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;
}
Mã sử dụng trong 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 sử dụng cho TextViewtxtCount
trong dự án của tôi là
<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>
Nhưng vẫn không may mắn ngay cả sau khi thiết lập cùng chiều rộng và chiều cao bằng 50. Thuộc tính vẫn hoạt động như hình bầu dục.
nếu 'width == height' thì Oval phải là Hình tròn? – Blackbelt
Nhưng tôi không nhận được một vòng tròn. Ví dụ nếu tôi vượt qua chiều rộng là 42 và chiều cao là 23, tôi đang nhận được vòng tròn. – Chandru
Hiển thị mã sử dụng ShapeDrawable này. Kích thước của Chế độ xem sử dụng nó là gì? – Karakuri