在android中以编程方式绘制圆形

Asl*_*zov 4 java android android-custom-view android-drawable

我想做的是画一个圆圈并用一种颜色(例如橙色)填充它,并希望以编程方式用另一种颜色(蓝色)制作边框。我没有找到任何关于如何执行此操作的教程。

这就是我想要得到的:

在此输入图像描述

Rea*_*hed 11

要以编程方式实现可绘制的圆圈,您需要具有如下所示的函数。

public static GradientDrawable drawCircle(int backgroundColor, int borderColor) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.OVAL);
    shape.setCornerRadii(new float[]{0, 0, 0, 0, 0, 0, 0, 0});
    shape.setColor(backgroundColor);
    shape.setStroke(10, borderColor);
    return shape;
}
Run Code Online (Sandbox Code Playgroud)

并按如下drawable所示设置。ImageView

imageView.setBackground(drawCircle(getResources().getColor(android.R.color.holo_blue_dark), getResources().getColor(android.R.color.holo_red_dark)));
Run Code Online (Sandbox Code Playgroud)

这是给予这样的东西。

在此输入图像描述