如何在android中用多种颜色表示圆形边框

如何在android中用多种颜色表示圆形边框,android,Android,我正在寻找一个自定义小部件绘制一个具有多种边框颜色的圆圈 例如,如果我的整个圆表示0-360,我需要用不同的颜色为我的圆边框上色 例如,我需要用红色标记0-60,用绿色标记61-120,用洋红标记121-300,用黄色边框标记301-360 请建议我如何在android中实现它。为此,您可以尝试我遇到的这个库 可能有用 使用固定长度的自定义视图,通过使用画布渲染圆,应用程序非常简单。我不建议您使用外部库。可以快速实现绘制和管理所需形状的类。举一个例子: public class Differen

我正在寻找一个自定义小部件绘制一个具有多种边框颜色的圆圈

例如,如果我的整个圆表示0-360,我需要用不同的颜色为我的圆边框上色

例如,我需要用红色标记0-60,用绿色标记61-120,用洋红标记121-300,用黄色边框标记301-360


请建议我如何在android中实现它。

为此,您可以尝试我遇到的这个库 可能有用


使用固定长度的自定义视图,通过使用画布渲染圆,应用程序非常简单。我不建议您使用外部库。可以快速实现绘制和管理所需形状的类。举一个例子:

public class DifferentColorCircularBorder{

    private RelativeLayout parentLayout;

    public DifferentColorCircularBorder(RelativeLayout parentLayout) {
        this.parentLayout = parentLayout;
    }

    public void addBorderPortion(Context context, int color, int startDegree, int endDegree) {
        ProgressBar portion = getBorderPortion(context, color, startDegree, endDegree);
        parentLayout.addView(portion);
    }

    private ProgressBar getBorderPortion(Context context, int color, int startDegree, int endDegree) {
        LayoutInflater inflater = LayoutInflater.from(context);

        ProgressBar portion = (ProgressBar) inflater.inflate(R.layout.border_portion, parentLayout, false);
        portion.setRotation(startDegree);
        portion.setProgress(endDegree - startDegree);

        portion.getProgressDrawable().setColorFilter(color, Mode.SRC_ATOP);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) portion.getLayoutParams();
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        portion.setLayoutParams(params);

        return portion;
    }

}
边框部分
定义如下:

<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="220dp"
    android:layout_height="220dp"
    android:progressDrawable="@drawable/circle_exterior"
    android:layout_centerInParent="true"
    android:max="360"/>
MainActivity
类的定义如下:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout interiorLayout = (RelativeLayout) findViewById(R.id.interior);

        DifferentColorCircularBorder border = new DifferentColorCircularBorder(interiorLayout);
        border.addBorderPortion(getApplicationContext(), Color.RED, 0, 40);
        border.addBorderPortion(getApplicationContext(), Color.GREEN, 40, 90);
        border.addBorderPortion(getApplicationContext(), Color.BLUE, 90, 270);
        border.addBorderPortion(getApplicationContext(), 0xFF123456, 270, 360);

    }
}
最后,
activity\u main
布局为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <RelativeLayout
        android:id="@+id/interior"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <View
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@drawable/circle_interior_bg"
            android:layout_centerInParent="true" />

    </RelativeLayout>

</RelativeLayout>

有关尺寸的说明:这是一个示例。在这里,我已经选择了与圆完美匹配的尺寸。根据您的应用程序更改这些设置

图像样本

我刚刚为此创建了一个简单的库,它的灵感来自WhatsApp Status,并且易于使用

首先添加视图,在我的例子中,我已经在
CircleImageView
周围添加了它,但是您可以在任何视图上使用它

<RelativeLayout
        android:id="@+id/image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_centerInParent="true"
            android:padding="6dp"
            android:src="@mipmap/ic_launcher" />

        <com.devlomi.circularstatusview.CircularStatusView
            android:id="@+id/circular_status_view"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_centerInParent="true"
            app:portion_color="@color/colorAccent"
            app:portion_spacing="4dp"
            app:portion_width="4dp"
            app:portions_count="8" />
    </RelativeLayout>

您可以使用以下命令以编程方式设置部分计数:

circularStatusView.setportionscont(count)

对于颜色部分:

circarStatusView.setPortionsColor(颜色)

还可以为每个部分设置特定颜色:


circularStatusView.setPortionColorForIndex(/*从顶部CW*/i的第一部分开始的部分索引,颜色)

您是否正在寻找与此相关的完整圆?我可以用这种方式应用渐变颜色吗?@venky是的,您可以应用渐变颜色。Bt唯一的问题是你只能使用三种颜色
什么是圆形\u interior\u bg???@PankajAndroid你可以删除它,这是默认设置
<RelativeLayout
        android:id="@+id/image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_centerInParent="true"
            android:padding="6dp"
            android:src="@mipmap/ic_launcher" />

        <com.devlomi.circularstatusview.CircularStatusView
            android:id="@+id/circular_status_view"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_centerInParent="true"
            app:portion_color="@color/colorAccent"
            app:portion_spacing="4dp"
            app:portion_width="4dp"
            app:portions_count="8" />
    </RelativeLayout>