Cha*_*dru 9 android xml-drawable android-canvas shapedrawable android-drawable
我的项目要求在运行时动态绘制一个圆.所以为了这个目的,我使用ShapeDrawable以编程方式创建圆,但不幸的是我在CircleShape的ShapeDrawable中找不到任何类或方法,而只是我找到了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后仍然没有运气.该属性仍然表现为椭圆形.
这已经来不及回答,但希望它会帮助别人.如果想像这样绘制圆圈,不要打扰46.0%,因为它只是文本视图.
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);
}
}
因此,在xml文件中使用此类,因为它是一个视图类.
给你相同的高度和宽度
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"
            />
| 归档时间: | 
 | 
| 查看次数: | 18169 次 | 
| 最近记录: |