首页 文章

如何在android中创建循环ProgressBar? [关闭]

提问于
浏览
149

您是否知道如何制作像Google Fit应用程序一样的循环进度条?如下图所示 .

enter image description here

2 回答

  • 126

    你可以尝试这个Circle Progress

    enter image description here

    enter image description here

    NB: 请始终为进度视图使用相同的宽度和高度

    DonutProgress:

    <com.github.lzyzsd.circleprogress.DonutProgress
            android:id="@+id/donut_progress"
            android:layout_marginLeft="50dp"
            android:layout_width="100dp"
            android:layout_height="100dp"
            custom:circle_progress="20"/>
    

    CircleProgress:

    <com.github.lzyzsd.circleprogress.CircleProgress
            android:id="@+id/circle_progress"
            android:layout_marginLeft="50dp"
            android:layout_width="100dp"
            android:layout_height="100dp"
            custom:circle_progress="20"/>
    

    ArcProgress:

    <com.github.lzyzsd.circleprogress.ArcProgress
            android:id="@+id/arc_progress"
            android:background="#214193"
            android:layout_marginLeft="50dp"
            android:layout_width="100dp"
            android:layout_height="100dp"
            custom:arc_progress="55"
            custom:arc_bottom_text="MEMORY"/>
    
  • 330

    It's easy to create this yourself

    在您的布局中包含以下具有特定drawable的 ProgressBar (请注意,您应该从尺寸中获取宽度) . 最大值在这里很重要:

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:max="500"
        android:progress="0"
        android:progressDrawable="@drawable/circular" />
    

    现在使用以下形状在资源中创建drawable . 使用半径(您可以使用 innerRadius 而不是 innerRadiusRatio )和厚度值进行游戏 .

    circular (Pre Lollipop OR API Level < 21)

    <shape
            android:innerRadiusRatio="2.3"
            android:shape="ring"
            android:thickness="3.8sp" >
            <solid android:color="@color/yourColor" />
       </shape>
    

    circular ( >= Lollipop OR API Level >= 21)

    <shape
            android:useLevel="true"
            android:innerRadiusRatio="2.3"
            android:shape="ring"
            android:thickness="3.8sp" >
            <solid android:color="@color/yourColor" />
         </shape>
    

    默认情况下,API Level 21(Lollipop)中的useLevel为 "false" .

    Start Animation

    在代码中接下来使用 ObjectAnimator 来为布局的 ProgessBar 的进度字段设置动画 .

    ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 0, 500); // see this max value coming back here, we animate towards that value
    animation.setDuration(5000); // in milliseconds
    animation.setInterpolator(new DecelerateInterpolator());
    animation.start();
    

    Stop Animation

    progressBar.clearAnimation();
    

    附:与上面的例子不同,它提供了流畅的动画

相关问题