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

问题描述 投票:0回答:1

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

这就是我想得到的:

enter image description here

android android-custom-view android-drawable
1个回答
0
投票

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

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

并在你的drawable中设置ImageView,如下所示。

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

这是给这样的东西。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.