Android实用笔记——使用ProgressBar实现进度条

1、ProgressBar的分类

    可以精确显示进度(可以显示刻度或者精确百分比)

222858_jplA_2725918.png

    不可以精确显示精度(一直转,类似于一个过场动画)

222906_s357_2725918.png

 

2、关键属性和方法

    指定ProgressBar显示风格

        style="?android:attr/progressBarStyleLarge"    大环形进度条

        style="?android:attr/progressBarStyleSmall"    小环形进度条

        style="?android:attr/progressBarStyleHorizontal"    水平进度条

    ProgressBar的关键属性

        android:max="100"    最大显示进度

        android:progress="50"    第一显示进度

        android:secondaryProgress="80"    第二显示进度

        android:indenterminate="true"    设置是否精确显示(true表示不精确显示进度,false表示精确显示进度)

    ProgressBar的关键方法

        setProgress(int)    设置第一进度

        setSecondaryProgress(int)    设置第二进度

        setProgress()    获取第一进度

        getSecondaryProgress()    获取第二进度

        incrementProgressBy(int)    增加或减少第一进度

        incrementSecondaryProgressBy(int)    增加或减少第二进度

        getMax()    获取最大进度

 

3、标题栏中的ProgressBar

package com.example.myandroidprogressbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //1、启用窗口特征,启用带进度何不带进度的两种进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        
        setContentView(R.layout.activity_main);
        
        //显示两种进度条
        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(true);
        //Max=10000
        setProgress(600);
    }
}

222958_SFgL_2725918.png

    

4、界面中四种不同形式的进度条

    修改activity_main.xml文件如下

<RelativeLayout 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" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/progressBar1"
        android:layout_toRightOf="@+id/progressBar1" />
    
    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/horiz"
        android:layout_toRightOf="@+id/progressBar2" />

    <ProgressBar
        android:id="@+id/horiz"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBar1"
        android:layout_below="@+id/progressBar1"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="80" />
      
</RelativeLayout>

234312_dSrB_2725918.png

 

5、通过按钮改变进度条的数值并动态改变

    修改activity_main.xml文件

<RelativeLayout 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" >

    <ProgressBar
        android:id="@+id/horiz"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="80" />
    
    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/horiz"
        android:layout_below="@+id/horiz"
        android:text="@string/add" />

    <Button
        android:id="@+id/reduce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/add"
        android:layout_below="@+id/add"
        android:text="@string/reduce" />

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/reduce"
        android:layout_below="@+id/reduce"
        android:text="@string/reset" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/reset"
        android:layout_alignRight="@+id/horiz"
        android:layout_below="@+id/reset"
        android:layout_marginTop="16dp" />


</RelativeLayout>

    修改MainActivity.java文件

package com.example.myandroidprogressbar;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
	//1、声明部分控件
	private ProgressBar progress;
	private Button add;
	private Button reduce;
	private Button reset;
	private TextView text;	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        
        setContentView(R.layout.activity_main);

        //2、初始化控件
        init();
    }

	private void init() {
		// TODO Auto-generated method stub
		//3、绑定
		progress=(ProgressBar) findViewById(R.id.horiz);
		add=(Button) findViewById(R.id.add);
		reduce=(Button) findViewById(R.id.reduce);
		reset=(Button) findViewById(R.id.reset);
		text=(TextView) findViewById(R.id.text);
		
		//4、获取各进度条进度
		//获取第一进度条的进度
		//int first=progress.getProgress();
		//湖区第二进度条的进度
		//int second=progress.getSecondaryProgress();
		//获取进度条的最大进度
		//int max=progress.getMax();
		//5、将数据显示到textView中
		//text.setText("第一进度的百分比:"+(int)(first/(float)max*100)+
				//"% 第二进度的百分比:"+(int)(second/(float)max*100)+"%");
		add.setOnClickListener(this);
		reduce.setOnClickListener(this);
		reset.setOnClickListener(this);
	}

	//6、设置按钮点击响应事件,改变进度条数据
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.add:
			//增加第一进度和第二进度10个刻度
			progress.incrementProgressBy(10);
			progress.incrementSecondaryProgressBy(10);
			break;
			
		case R.id.reduce:
			//减少第一进度和第二进度10个刻度
			progress.incrementProgressBy(-10);
			progress.incrementSecondaryProgressBy(-10);
			break;
			
		case R.id.reset:
			//重置
			progress.setProgress(50);
			progress.setSecondaryProgress(80);		
			break;
			
		}
		//7、因为每一次点击都会调用onClick这个函数,并且一定会执行switch这个判断。
		//所以没必要在每一个分支中刷新textView的内容,只需要在switch分支外面写就好了,
		//之前的向textView中写入数据的应该注释掉
		text.setText("第一进度的百分比:"+(int)(progress.getProgress()/(float)progress.getMax()*100)+
				"% 第二进度的百分比:"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%");
	}
}

004203_5c2u_2725918.png

004225_9oeE_2725918.png

004239_TT7J_2725918.png

 

6、对话框形式的进度条(ProgressDialog)

    修改activity_main.xml文件

<RelativeLayout 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" >

    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp"
        android:text="显示进度条对话框" />

</RelativeLayout>

    修改MainActivity.java文件

package com.example.myandroidprogressdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
	//1、声明progressDialog的对象以及按钮
	private ProgressDialog prodialog;
	private Button show;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        init();
    }

	private void init() {
		// TODO Auto-generated method stub
		//2、绑定
		show=(Button) findViewById(R.id.show);
		//3、设置监听器
		show.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		//4、设置页面显示风格
		//新建progressDialog对象
		prodialog=new ProgressDialog(MainActivity.this);
		//给对话框设置显示风格
		prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		//设置标题
		prodialog.setTitle("哈哈哈");
		//设置对话框里的而文字信息
		prodialog.setMessage("我好聪明啊");
		//设置图标
		prodialog.setIcon(R.drawable.ic_launcher);
		
		//5、设定关于progressBar的一些属性
		//设定最大进度
		prodialog.setMax(100);
		//设定初始化进度
		prodialog.incrementProgressBy(50);
		//进度条是明确显示进度的
		prodialog.setIndeterminate(false);
		
		//6、设定一个确定按钮
		prodialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this,"快点爱上我", Toast.LENGTH_SHORT);
			}
		});
		
		//7、是否可以通过返回按钮退出对话框
		prodialog.setCancelable(true);
		
		//8、显示progressDialog
		prodialog.show();
	}
    
}

011252_eNBC_2725918.png

011300_wObr_2725918.png

 

7、自定义ProgressBar的样式

    首先我们需要知道系统预定义的水平进度条的样式是如何加载的。这样我们需要回到activity_main.xml文件中来。

    我们注意到下面一行代码:

        style="?android:attr/progressBarStyleHorizontal"

    事实上,他真正的加载文件位于

        style="@android:style/Widget.ProgressBar.Horizontal"

    我们通过ctrl+鼠标左键点击引号中的位置,就可以看到系统自带的横向进度条的样式

    <style name="Widget.ProgressBar.Horizontal">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
        <item name="android:minHeight">20dip</item>
        <item name="android:maxHeight">20dip</item>
        <item name="android:mirrorForRtl">true</item>
    </style>

    其中的android:progressDrawable可以去加载一个自己自定义的布局样式的一个文件。我们同样用ctrl+鼠标左键打开这里的样式文件,看看系统是如何设定的

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--背景  -->
    <item android:id="@android:id/background">
        <shape>
            <!--圆角  -->
            <corners android:radius="5dip" />
            <!--渐变色  -->
            <gradient
                    <!--起始颜色  -->
                    android:startColor="#ff9d9e9d"
                    <!--中间颜色  -->
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    <!--终止颜色  -->
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>
    
    <!--第二进度条  -->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
    <!--第一进度条  -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerY="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
</layer-list>

    不难发现,我们可以自己对他的颜色进行修改,以达到自定义的目的。这里我简单把颜色修改了一下

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--背景  -->
    <item android:id="@android:id/background">
        <shape>
            <!--圆角  -->
            <corners android:radius="5dip" />
            <!--渐变色  -->
            <gradient
                    <!--起始颜色  -->
                    android:startColor="#B9A4FF"
                    <!--中间颜色  -->
                    android:centerColor="#C6B7FF"
                    android:centerY="0.75"
                    <!--终止颜色  -->
                    android:endColor="#C3B2FF"
                    android:angle="270"
            />
        </shape>
    </item>
    
    <!--第二进度条  -->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#57E8FF"
                        android:centerColor="#74EBFF"
                        android:centerY="0.75"
                        android:endColor="#8EEFFF"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
    <!--第一进度条  -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerY="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
</layer-list>

    运行即可得到自定义进度条的结果,可自行与本文之前的截图对比,可以看到很明显的差别,尽管颜色丑了点。

013724_bKTV_2725918.png

    希望大家能以小见大,做一些更有意思的尝试。

转载于:https://my.oschina.net/CoderBleak/blog/718751

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值