5

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.

+0

nếu 'width == height' thì Oval phải là Hình tròn? – Blackbelt

+0

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

+0

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

Trả lời

1

Give cùng chiều cao và chiều rộng để bạn TextView

<TextView 
      android:id="@+id/txt_count" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:textColor="@color/text_grey" 
      android:gravity="center" 
      android:textSize="12sp" 
      android:padding="2dp" 
      /> 
+0

cha mẹ của 'txtCount' là gì? – dhams

+0

Bạn nên đặt cùng chiều cao và chiều rộng cho Textview – dhams

+0

Cảm ơn bạn đã làm việc! Thực sự hữu ích để khắc phục sai lầm ngớ ngẩn của tôi trong xml. Tôi chấp nhận giải pháp này – Chandru

-3
// Circle 

    Paint paint = new Paint(); 
    paint.setColor(Color.GREEN); 
    paint.setStyle(Paint.Style.STROKE); 
    float x = 50; 
    float y = 50; 
    float radius = 20; 
    canvas.drawCircle(x, y, radius, paint); 
1

Đây là quá muộn để trả lời nhưng hy vọng nó sẽ giúp người khác. Nếu muốn vẽ vòng tròn như thế này, đừng bận tâm với 46.0% vì nó chỉ là dạng xem văn bản.

enter image description here.

public class Circle extends View { 

private Paint mCircleYellow; 
private Paint mCircleGray; 

private float mRadius; 
private RectF mArcBounds = new RectF(); 

public Circle(Context context) { 
    super(context); 

    // create the Paint and set its color 

} 

public Circle(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 

    initPaints(); 
} 

public Circle(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

private void initPaints() { 
    mCircleYellow = new Paint(Paint.ANTI_ALIAS_FLAG); 
    mCircleYellow.setStyle(Paint.Style.FILL); 
    mCircleYellow.setColor(Color.YELLOW); 
    mCircleYellow.setStyle(Paint.Style.STROKE); 
    mCircleYellow.setStrokeWidth(15 * getResources().getDisplayMetrics().density); 
    mCircleYellow.setStrokeCap(Paint.Cap.SQUARE); 
    // mEyeAndMouthPaint.setColor(getResources().getColor(R.color.colorAccent)); 
    mCircleYellow.setColor(Color.parseColor("#F9A61A")); 

    mCircleGray = new Paint(Paint.ANTI_ALIAS_FLAG); 
    mCircleGray.setStyle(Paint.Style.FILL); 
    mCircleGray.setColor(Color.GRAY); 
    mCircleGray.setStyle(Paint.Style.STROKE); 
    mCircleGray.setStrokeWidth(15 * getResources().getDisplayMetrics().density); 
    mCircleGray.setStrokeCap(Paint.Cap.SQUARE); 
    // mEyeAndMouthPaint.setColor(getResources().getColor(R.color.colorAccent)); 
    mCircleGray.setColor(Color.parseColor("#76787a")); 

} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 

    mRadius = Math.min(w, h)/2f; 

} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int w = MeasureSpec.getSize(widthMeasureSpec); 
    int h = MeasureSpec.getSize(heightMeasureSpec); 

    int size = Math.min(w, h); 
    setMeasuredDimension(size, size); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    Float drawUpto = 46f; 


    float mouthInset = mRadius/3f; 
    mArcBounds.set(mouthInset, mouthInset, mRadius * 2 - mouthInset, mRadius * 2 - mouthInset); 
    canvas.drawArc(mArcBounds, 0f, 360f, false, mCircleGray); 

    canvas.drawArc(mArcBounds, 270f, drawUpto, false, mCircleYellow); 


} 

}

Vì vậy, sử dụng lớp này trong file xml của bạn vì nó là một lớp xem.

Các vấn đề liên quan