使用progressBar实现加载动画和加载进度条——小白必备(五)

关键函数
progressBar加载控件的使用
max 最大值
getProgress 进度条
indeterminate 水平进度条

package com.example.imageview;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    
    
    private  ProgressBar  ProgressBar;
    private  ProgressBar  ProgressBar2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       ProgressBar =  findViewById(R.id.pd);
        ProgressBar2 =  findViewById(R.id.pd2);
    }
    public void leoClick(View view) {
    
    
        if (ProgressBar.getVisibility() == View.GONE) {
    
    
            ProgressBar.setVisibility(View.VISIBLE);
        } else {
    
    
            ProgressBar.setVisibility(View.GONE);
        }
    }
    public void load(View view) {
    
    
        int progress = ProgressBar2.getProgress();
        progress +=10;
        ProgressBar2.setProgress(progress);
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/ic_baseline_directions_walk_24"
    android:scaleType="center"
android:adjustViewBounds="true"
    />
    <ProgressBar
        android:id="@+id/pd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:text="显示隐藏的进度条"
        android:layout_width="wrap_content"
        android:onClick="leoClick"
        android:layout_height="wrap_content"
        />
    <ProgressBar
        android:id="@+id/pd2"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:layout_width="300dp"
        android:layout_height="wrap_content"/>
    <Button
        android:text="模拟下载"
        android:onClick="load"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_44584479/article/details/115177948