在Android开发中如何实现好看的进度条

发布时间:2022-02-25 14:38:08 作者:小新
来源:亿速云 阅读:128

这篇文章主要为大家展示了“在Android开发中如何实现好看的进度条”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“在Android开发中如何实现好看的进度条”这篇文章吧。

activity_main.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:background="#ffffff"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="16dp">
        <TextView
            android:id="@+id/textView17"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textAlignment="center"
            android:text="\u00A9  itinsidenews.com" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Determinate" />
            <ProgressBar
                android:id="@+id/progressDeterminate"
                
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="false"
                android:max="100"
                android:progress="10" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Indeterminate" />
            <ProgressBar
                android:id="@+id/progressIndeterminate"
                
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="true" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Buffer" />
            <ProgressBar
                android:id="@+id/progressBuffered"
                
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="false"
                android:max="100"
                android:progress="10"
                android:secondaryProgress="20" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Indeterminate and Determinate" />
            <ProgressBar
                android:id="@+id/progressIndeterminateDeterminate"
                
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="false"
                android:max="100"
                android:progress="20" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="15dp"
                    android:text="Indeterminate" />
                <ProgressBar
                    
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:indeterminate="true"
                   android:progressDrawable="@drawable/circular_progress_bar" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="15dp"
                    android:text="Determine" />
                <ProgressBar
                    android:id="@+id/progressIndeterminateCircular"
                    
                    android:layout_width="?attr/actionBarSize"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@drawable/circle_shape"
                    android:indeterminate="false"
                    android:max="100"
                    android:progress="0"
                   android:progressDrawable="@drawable/circular_progress_bar" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

MainActivity Java 文件代码

在MainActivity.java文件中,我们的第一步是初始化所有的进度条视图,然后编写三个void类型的函数,分别为每个进度条编写代码。 

我们在每个函数中使用了一个处理程序。在Android中,我们不能在主线程上运行长期任务;这就是我们使用处理程序的原因。处理程序允许从其他后台线程与 UI 线程进行通信。 

MainActivity.java
package com.progressbar.example.mainactivity;import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.progressbar.example.R;
import com.progressbar.example.utils.Tools;
public class MainActivity extends AppCompatActivity {
    private ProgressBar progressDeterminate;
    private ProgressBar progressIndeterminateCircular;
    private ProgressBar progressBuffered;
    private ProgressBar progressIndeterminateDeterminate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
        initComponent();
    }
     private void initComponent() {
        progressDeterminate = (ProgressBar) findViewById(R.id.progressDeterminate);
        progressIndeterminateCircular = (ProgressBar) findViewById(R.id.progressIndeterminateCircular);
        progressBuffered = (ProgressBar) findViewById(R.id.progressBuffered);
        progressIndeterminateDeterminate = (ProgressBar) findViewById(R.id.progressIndeterminateDeterminate);
        startDeterminateProgress();
        startBufferedProgress();
        startBufferedSecondaryProgress();
        startIndeterminateDeterminateProgress();
        startDeterminateCircularProgress();
    }
    private void startDeterminateProgress() {
        final Handler mHandler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                int progress = progressDeterminate.getProgress() + 10;
                progressDeterminate.setProgress(progress);
                if (progress > 100) {
                    progressDeterminate.setProgress(0);
                }
                mHandler.postDelayed(this, 1000);

以上是“在Android开发中如何实现好看的进度条”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 如何使用JavaScript实现好看的跟随彩色气泡效果
  2. 在python tkinter中Canvas如何实现进度条显示

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:在Android中如何实现扫描和生成二维码

下一篇:使用android布局如何进行优化

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》