700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android进度条函数 Android实用笔记——使用ProgressBar实现进度条

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

时间:2020-11-04 21:31:48

相关推荐

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

1、ProgressBar的分类

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

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

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);

}

}

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

修改activity_main.xml文件如下

xmlns:tools="/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: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" />

android:id="@+id/progressBar2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/progressBar1"

android:layout_toRightOf="@+id/progressBar1" />

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

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

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

修改activity_main.xml文件

xmlns:tools="/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: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" />

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

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

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

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

修改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)+"%");

}

}

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

修改activity_main.xml文件

xmlns:tools="/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: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="显示进度条对话框" />

修改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();

}

}

7、自定义ProgressBar的样式

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

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

style="?android:attr/progressBarStyleHorizontal"

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

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

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

false

@android:drawable/progress_horizontal

@android:drawable/progress_indeterminate_horizontal

20dip

20dip

true

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

android:startColor="#ff9d9e9d"

android:centerColor="#ff5a5d5a"

android:centerY="0.75"

android:endColor="#ff747674"

android:angle="270"

/>

android:startColor="#80ffd300"

android:centerColor="#80ffb600"

android:centerY="0.75"

android:endColor="#a0ffcb00"

android:angle="270"

/>

android:startColor="#ffffd300"

android:centerColor="#ffffb600"

android:centerY="0.75"

android:endColor="#ffffcb00"

android:angle="270"

/>

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

android:startColor="#B9A4FF"

android:centerColor="#C6B7FF"

android:centerY="0.75"

android:endColor="#C3B2FF"

android:angle="270"

/>

android:startColor="#57E8FF"

android:centerColor="#74EBFF"

android:centerY="0.75"

android:endColor="#8EEFFF"

android:angle="270"

/>

android:startColor="#ffffd300"

android:centerColor="#ffffb600"

android:centerY="0.75"

android:endColor="#ffffcb00"

android:angle="270"

/>

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。