首页 > 解决方案 > 在android中以编程方式绘制圆形

问题描述

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

这就是我想要得到的:

在此处输入图像描述

标签: javaandroidandroid-custom-viewandroid-drawable

解决方案


要以编程方式实现圆形可绘制,您需要具有如下功能。

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;
}

并像下面这样设置drawableImageView的。

imageView.setBackground(drawCircle(getResources().getColor(android.R.color.holo_blue_dark), getResources().getColor(android.R.color.holo_red_dark)));

这是给这样的东西。

在此处输入图像描述


推荐阅读