Android学习笔记3-3
推荐新手向学习视频:B站https://www.bilibili.com/video/av38409964点我传送
3-3 ProgressBar & ProgressDialog(加载进度条、转圈圈)
今天下了一个叫ScreenToGif的软件,可以截取动图了,啦啦啦
-
一些未展示属性
- android:secondaryProgress=“30” 设置二级进度条为30
- android:indeterminateDrawable="@drawable/bg_progress" 设置自定义样式
-
activity_progress.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:gravity="center_horizontal" android:padding="15dp"> <ProgressBar android:id="@+id/pb1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/pb2" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar" android:layout_marginTop="10dp"/> <ProgressBar android:id="@+id/pb3" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_marginTop="10dp" android:max="100" android:progress="10" android:secondaryProgress="30"/> <ProgressBar android:id="@+id/pb4" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.Material.ProgressBar.Horizontal" android:layout_marginTop="10dp" android:max="100" android:progress="10" android:secondaryProgress="30"/> <Button android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="模拟进度条" android:layout_marginTop="10dp"/> <ProgressBar android:id="@+id/pb5" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/MyProgressBar" 自定义效果 android:layout_marginTop="10dp"/> <Button android:id="@+id/btn_progress_dialog1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ProgressDialog1" android:textAllCaps="false" android:layout_marginTop="10dp"/> <Button android:id="@+id/btn_progress_dialog2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ProgressDialog2" android:textAllCaps="false" android:layout_marginTop="10dp"/> </LinearLayout>
-
效果
-
drawable下的自定义布局bg_progress.xml文件
<?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/icon_progress" android:pivotX="50%" android:pivotY="50%"> <!--两个50%确定图片的旋转中心--> </animated-rotate>
-
styles.xml文件里添加自定义样式
<style name="MyProgressBar"> <item name="android:indeterminateDrawable">@drawable/bg_progress</item> </style>
-
ProgressActivity.java文件
package com.ylw.helloworld; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import com.ylw.helloworld.util.ToastUtil; public class ProgressActivity extends AppCompatActivity { private ProgressBar mPb3; private Button mBtnStart,mBtnProgressDialog1,mBtnProgressDialog2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress); mPb3 = findViewById(R.id.pb3); mBtnStart = findViewById(R.id.btn_start); mBtnProgressDialog1 = findViewById(R.id.btn_progress_dialog1); mBtnProgressDialog2 = findViewById(R.id.btn_progress_dialog2); mPb3.setProgress(30); //设置进度条到30 mBtnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handler.sendEmptyMessage(0); } }); mBtnProgressDialog1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //虽然标了中划线但还是可以用的 ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this); progressDialog.setTitle("提示"); progressDialog.setMessage("正在加载"); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { //监听取消 @Override public void onCancel(DialogInterface dialog) { ToastUtil.showMsg(ProgressActivity.this,"cancel..."); } }); progressDialog.setCancelable(false); //设置不可以被取消 progressDialog.show(); } }); mBtnProgressDialog2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //虽然标了中划线但还是可以用的 ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置样式为进度条 progressDialog.setTitle("提示"); progressDialog.setMessage("正在下载..."); //设置按钮和点击事件 progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "等一会", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // } }); progressDialog.show(); } }); } Handler handler = new Handler(){ //容器 @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if (mPb3.getProgress()<100){ //如果进度小于100 handler.postDelayed(runnable,500); //延迟500毫秒发出消息 }else{ ToastUtil.showMsg(ProgressActivity.this,"加载完成"); } } }; Runnable runnable = new Runnable() { //线程 @Override public void run() { mPb3.setProgress(mPb3.getProgress()+5); //把当前进度+5 handler.sendEmptyMessage(0); } }; }
-
效果(今天下了一个叫ScreenToGif的软件,可以截取动图了,啦啦啦)
-
模拟进度条效果
-
ProgressDialog1效果
-
ProgressDialog2效果