简体   繁体   English

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

[英]Draw circle shape programmatically in android

What I want to do is to draw a circle and fill it by one color (for example orange) and want to make a border in another color (blue) programmatically. 我想做的是画一个圆,并用一种​​颜色(例如橙色)填充它,并想以编程方式用另一种颜色(蓝色)做一个边框。 I didn't find any tutorial on how to do this. 我没有找到有关如何执行此操作的任何教程。

This is what I want to get: 这就是我想要得到的:

在此处输入图片说明

To achieve the circle drawable programmatically you need to have a function like the following. 要以编程方式实现可绘制圆形,您需要具有以下功能。

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

And set the drawable in your ImageView like the following. 并如下所示在ImageView设置drawable

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

This is giving something like this. 这就是这样的事情。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM