Android之自定义ProgressBar
16lz
2021-12-04
本文简单介绍下Android之自定义ProgressBar。
多的不说,先上图
布局文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:padding="5dp" /> <ProgressBar android:id="@+id/progress1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" /> <ProgressBar android:id="@+id/progress2" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="60" android:secondaryProgress="80" android:padding="5dp"/> <ProgressBar android:id="@+id/progress3" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="60" android:secondaryProgress="80" android:progressDrawable="@drawable/progress_horizontal" android:padding="5dp"/> <ProgressBar android:id="@+id/progress4" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="25dp" android:max="100" android:progress="80" android:paddingLeft="1dp" android:paddingRight="1dp" android:progressDrawable="@drawable/progressbar_layer_list" android:padding="5dp"/> <ProgressBar android:id="@+id/progress5" android:layout_width="30dip" android:layout_height="30dip" android:indeterminateDrawable="@drawable/progress_selector" android:visibility="visible" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="progress value" android:padding="5dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="value -"/> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="value +"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second progress value" android:padding="5dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="value -"/> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="value +"/> </LinearLayout></LinearLayout>
布局文件关联的文件
progress_horizontal.xml
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background" android:drawable="@color/red"> </item> <item android:id="@android:id/secondaryProgress"> <scale android:drawable="@color/green" android:scaleWidth="100%" /> </item> <item android:id="@android:id/progress"> <scale android:drawable="@color/blue" android:scaleWidth="100%" /> </item></layer-list>
progressbar_layer_list.xml
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@android:id/background"android:drawable="@drawable/pic2"></item><itemandroid:id="@android:id/progress"android:drawable="@drawable/pic1"></item></layer-list>
progress_selector.xml
<?xml version="1.0" encoding="UTF-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"android:pivotX="50%" android:pivotY="50%"android:fromDegrees="0"android:toDegrees="360"><shapeandroid:shape="ring"android:innerRadiusRatio="3"android:thicknessRatio="8"android:useLevel="false"><gradient android:type="sweep"android:useLevel="false"android:startColor="#871318"android:centerColor="#D5202A"android:centerY="0.50"android:endColor="#FFEEEE"/></shape></rotate>
主程序
package com.sl.progressbardemo;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ProgressBar;import android.app.Activity;public class MainActivity extends Activity{private ProgressBar mProgressBar2;private ProgressBar mProgressBar3;private ProgressBar mProgressBar4;private Button mButton1;private Button mButton2;private Button mButton3;private Button mButton4;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_PROGRESS);setContentView(R.layout.activity_main);setProgressBarVisibility(true);mProgressBar2 = (ProgressBar)findViewById(R.id.progress2);mProgressBar3 = (ProgressBar)findViewById(R.id.progress3);mProgressBar4 = (ProgressBar)findViewById(R.id.progress4);mButton1 = (Button)findViewById(R.id.btn1);mButton2 = (Button)findViewById(R.id.btn2);mButton3 = (Button)findViewById(R.id.btn3);mButton4 = (Button)findViewById(R.id.btn4);mButton1.setOnClickListener(listener);mButton2.setOnClickListener(listener);mButton3.setOnClickListener(listener);mButton4.setOnClickListener(listener);}Button.OnClickListener listener = new OnClickListener(){@Overridepublic void onClick(View v){Button button = (Button)v;switch (button.getId()){case R.id.btn1:mProgressBar2.incrementProgressBy(-5);mProgressBar3.incrementProgressBy(-5);mProgressBar4.incrementProgressBy(-5);break;case R.id.btn2:mProgressBar2.incrementProgressBy(5);mProgressBar3.incrementProgressBy(5);mProgressBar4.incrementProgressBy(5);break;case R.id.btn3:mProgressBar2.incrementSecondaryProgressBy(-5);mProgressBar3.incrementSecondaryProgressBy(-5);break;case R.id.btn4:mProgressBar2.incrementSecondaryProgressBy(5);mProgressBar3.incrementSecondaryProgressBy(5);break;default:break;}}};}
源码下载
更多相关文章
- Android应用程序键盘(Keyboard)消息处理机制分析(12)
- android客户端利用sokcet通信和向Java服务端发请求,Java服务端把
- android ProgressBar 的使用
- android getResources的作用
- Android(安卓)文件的保存与读取之SDCard(SD卡)存储
- Android(安卓)DEV : setOnClickListener() vs. android:onClick
- 获取apk信息工具(android SDK的aapt工具)
- 开发可统计单词个数的Android驱动程序(1)
- Android(安卓)Layout布局文件里的android:layout_height等属性为