Android自定义进度条和SeekBar进度条背景色

一、自定义进度条

进度条对话框的布局代码如下所示:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="140dp"
              android:layout_height="140dp"
              android:background="@drawable/loading_shape"
              android:gravity="center"
              android:orientation="vertical"
    >

    <ProgressBar
        android:id="@+id/loading_progress"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:indeterminateDrawable="@drawable/progress_loading"/>

    <TextView
        android:id="@+id/tv_loading_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textColor="#fff"
        android:textSize="15sp"
        tools:text="加载中..."/>

LinearLayout>

1.1方式一

方式一:progress_loading.xml代码如下所示


<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    android:variablePadding="true"
    >
    <item android:duration="100">
        
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_1"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_2"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_3"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_4"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_5"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_6"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_7"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_8"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_9"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_10"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_11"
            android:gravity="left">clip>
    item>
    <item android:duration="100">
        <clip
            android:clipOrientation="horizontal"
            android:drawable="@drawable/qb_tenpay_loading_12"
            android:gravity="left">clip>
    item>

animation-list>
Android自定义进度条和SeekBar进度条背景色_第1张图片
Android4.4进度条效果图
Android自定义进度条和SeekBar进度条背景色_第2张图片
Android6.0以上进度条效果图

在Java代码中通过判断运行版本,在代码中适配Android6.0及以上系统的进度条。代码如下所示:

package study.com.progressbarandseekbardemo;

import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvStart;
    private TextView tvEnd;

    private Dialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvStart = (TextView) findViewById(R.id.tv_start);
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCustomProgressDialog();
            }
        });

        tvEnd = (TextView) findViewById(R.id.tv_end);
        tvEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopCustomProgressDialog();
            }
        });
    }

    private void showCustomProgressDialog() {
        showCustomProgressDialog("加载中...");
    }

    private void showCustomProgressDialog(String msg) {
        if (progressDialog == null) {
            progressDialog = new Dialog(this, R.style.ProgressDialogStyle);
        }
        progressDialog.setContentView(R.layout.dialog_custom_loading);
        progressDialog.setCancelable(true);
        progressDialog.getWindow().setBackgroundDrawableResource(
                android.R.color.transparent);

        ProgressBar progressBar = (ProgressBar) progressDialog.findViewById(R.id.loading_progress);
        //适配Android6.0及以上系统
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Drawable drawable = getApplicationContext().getResources().getDrawable(R.drawable.progress_loading_v23);
            progressBar.setIndeterminateDrawable(drawable);
        }

        TextView text = (TextView) progressDialog
                .findViewById(R.id.tv_loading_msg);
        text.setText(msg);
        progressDialog.show();
    }

    private void stopCustomProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}
修改之后的效果如下所示:
Android自定义进度条和SeekBar进度条背景色_第3张图片
Android6.0以上进度条效果图

1.2方式二

将进度条对话框布局文件中的android:indeterminateDrawable=”@drawable/progress_loading”改为android:indeterminateDrawable=”@drawable/progress_loading_v23”
方式二::progress_loading_v23.xml代码如下


<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:variablePadding="true"
    android:oneshot="false"
    >
    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_1"
          android:gravity="left"/>

    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_2"
          android:gravity="left"
    />

    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_3"
          android:gravity="left"
    />

    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_4"
          android:gravity="left"
    />

    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_5"
          android:gravity="left"
    />
    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_6"
          android:gravity="left"
    />
    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_7"
          android:gravity="left"
    />
    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_8"
          android:gravity="left"
    />
    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_9"
          android:gravity="left"
    />
    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_10"
          android:gravity="left"
    />
    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_11"
          android:gravity="left"
    />
    <item android:duration="100"
          android:drawable="@drawable/qb_tenpay_loading_12"
          android:gravity="left"
    />

animation-list>
Android自定义进度条和SeekBar进度条背景色_第4张图片
Android4.4进度条效果图
Android自定义进度条和SeekBar进度条背景色_第5张图片
Android6.0以上进度条效果图

由上面两张图可以看出,Android4.4上面存在重复影像

clip标签使用的方法如下:

Android中ClipDrawable的使用
android里面layer-list中的inset和clip到底有什么作用

二、自定义SeekBar

audio_seekbar.xml自定义SeekBar进度条的颜色,具体实现如下所示:


<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background"
        >

        <shape>
            <corners android:radius="5dip"/>

            <solid android:color="@color/third"/>
        shape>

    item>
    
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip"/>

                <solid android:color="@color/second"/>
            shape>
        clip>
    item>
    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip"/>

                <solid android:color="@color/first"/>
            shape>
        clip>
    item>
layer-list>

在布局文件中引用自定义进度条的颜色,具体代码如下所示:

 "@+id/sk_position"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20.0px"
        android:layout_marginRight="20.0px"
        android:max="100"
        android:maxHeight="20.0px"
        android:minHeight="20.0px"
        android:progress="12"
        android:secondaryProgress="34"
        android:progressDrawable="@drawable/audio_seekbar"
        />
效果图如下所示:
Android自定义进度条和SeekBar进度条背景色_第6张图片
SeekBar效果图

参考资料

自定义进度条

Android利用android:indeterminateDrawable来实现ProgressBar三种方式
ProgressBar的indeterminateDrawable属性在安卓6.0上的问题
Android ProgressBar控件理解

自定义SeekBar进度条背景色

Android中自定义SeekBar背景颜色,进度条颜色,滑块图片

项目源码

你可能感兴趣的:(Android技巧--对话框)