【问题标题】:Drawing Circle programmatically using Android ShapeDrawable使用 Android ShapeDrawable 以编程方式绘制圆
【发布时间】:2015-10-12 08:37:39
【问题描述】:

我的项目要求在运行时动态地画一个圆。因此,为此目的,我使用 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

<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 那么椭圆应该是一个圆?
  • 但我没有得到一个圆圈。例如,如果我将宽度设置为 42,高度设置为 23,我得到的是圆形。
  • 显示使用此 ShapeDrawable 的代码。使用它的 View 的大小是多少?

标签: android android-canvas android-drawable xml-drawable shapedrawable


【解决方案1】:

回答为时已晚,但希望对其他人有所帮助。如果想画这样的圆圈,不要费心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 文件中使用这个类,因为它是一个视图类。

【讨论】:

    【解决方案2】:

    为您的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"
                />
    

    【讨论】:

    • 'txtCount' 的父母是什么?
    • 您应该为 Textview 提供相同的高度和宽度
    • 谢谢它的工作!真的有助于纠正我在 xml 中的愚蠢错误。我接受这个解决方案
    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      相关资源
      最近更新 更多