如何使用圆形装载栏制作Android进度条?

时间:2012-02-01 15:25:14

标签: android styles progress-bar

如何在右侧(末端)制作带圆角的进度条,而不仅仅是在左侧(开始)。我目前所拥有的几乎就是我想要的布局,但进度条加载器只是一条直线垂直线,我想让这条线四舍五入。

3 个答案:

答案 0 :(得分:1)

基本上你应该制作一个自定义小部件,这样你就可以根据自己的喜好自定义它。

这是一个关于你正在寻找什么的教程。 link!

答案 1 :(得分:0)

所以我最终在xml中做了这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    android:id="@+id/splash_linear">
    <FrameLayout
        android:layout_width="144dp"
        android:layout_height="13dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp">
        <View android:id="@+id/progress_horizontal"
            android:background="@drawable/progress_background"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <View android:id="@+id/progress_horizontal_bar"
            android:background="@drawable/progress_bar"
            android:layout_width="0dp"
            android:layout_height="match_parent"/>
    </FrameLayout>
</LinearLayout>

然后在代码中:

public void updateProgress(int percent) {
    int progressBarSizeDp = 144;    // the size of the progressbar
    float scale = (float) (progressBarSizeDp/100.0);
    int progressSize = (int) (percent * scale);
    if(progressSize > progressBarSizeDp) {
        progressSize = progressBarSizeDp;
    } else if(progressSize < 20) {
        progressSize = 20;
    }

    View progressBar = (View) findViewById(R.id.progress_horizontal_bar);
    int py = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, progressSize, getResources().getDisplayMetrics());

    LayoutParams params = new FrameLayout.LayoutParams(py, LayoutParams.MATCH_PARENT);
    progressBar.setLayoutParams(params);
    View splashMain = (View) findViewById(R.id.splash_linear);
    splashMain.invalidate();
}

答案 2 :(得分:0)

找到一个很好的链接:

Custom progress bar with rounded corners

基本上它使用自定义RelativeLayout和9补丁方法绘制圆角进度条。

相关问题