android 垂直的进度条实现
来源:互联网 发布:软件产品包装方案 编辑:程序博客网 时间:2024/05/16 09:26
效果图:
利用TimerTask动态改变滚动条前景色
主要代码如下:
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/main_battery_bg"
- android:orientation="horizontal" >
- <ProgressBar
- android:id="@+id/pb_battery"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:indeterminate="false"
- android:indeterminateOnly="false"
- android:progress="0"
- android:progressDrawable="@drawable/main_progress_vertical" />
- </LinearLayout>
- </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/main_battery_bg" android:orientation="horizontal" > <ProgressBar android:id="@+id/pb_battery" android:layout_width="fill_parent" android:layout_height="fill_parent" android:indeterminate="false" android:indeterminateOnly="false" android:progress="0" android:progressDrawable="@drawable/main_progress_vertical" /> </LinearLayout></RelativeLayout>main_progress_vertical.xml
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background"> (这里设置的是进度条的外观颜色还有呈现的方向等)
<shape>
<corners android:radius="5dip" /><gradient
android:angle="180"
android:centerColor="#F6F6F6"
android:endColor="#C0C0C0"
android:startColor="#C0C0C0"
android:type="linear" />
</shape>
</item>- <item android:id="@android:id/progress"> (这里设置的是进度的呈现格式,有颜色的过度等,已经进度前进的方向)
- <clip
- android:clipOrientation="vertical"
- android:gravity="bottom" >
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:angle="180"
- android:centerColor="#ffffcb00"
- android:centerY="0.75"
- android:endColor="#ffffcb00"
- android:startColor="#ffffcb00" />
- </shape>
- </clip>
- </item>
- </layer-list>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/progress"> <clip android:clipOrientation="vertical" android:gravity="bottom" > <shape> <corners android:radius="5dip" /> <gradient android:angle="180" android:centerColor="#ffffcb00" android:centerY="0.75" android:endColor="#ffffcb00" android:startColor="#ffffcb00" /> </shape> </clip> </item></layer-list>
main_progress_vertical_warn.xml
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:id="@android:id/progress">
- <clip
- android:clipOrientation="vertical"
- android:gravity="bottom" >
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:angle="180"
- android:centerColor="#ff0000"
- android:centerY="0.75"
- android:endColor="#ff0000"
- android:startColor="#ff0000" />
- </shape>
- </clip>
- </item>
- </layer-list>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/progress"> <clip android:clipOrientation="vertical" android:gravity="bottom" > <shape> <corners android:radius="5dip" /> <gradient android:angle="180" android:centerColor="#ff0000" android:centerY="0.75" android:endColor="#ff0000" android:startColor="#ff0000" /> </shape> </clip> </item></layer-list>
MainActivity
- import java.util.Timer;
- import java.util.TimerTask;
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.drawable.Drawable;
- import android.widget.ProgressBar;
- public class MainActivity extends Activity {
- /**
- * 达到警告线
- */
- public static final int BATTERYWARN = 20;
- private ProgressBar pBar;
- /**
- * 需要改变多少
- */
- private int progressAdd = 0;
- /**
- * 改变是上升还是下降
- */
- private int actionType = 1;
- /**
- * 原来的progress值
- */
- private int progressOld = 0;
- private Timer timer = new Timer();
- /**
- * 动态改变滚动条的ProgressDrawable
- *
- * @param resId
- * Drawable资源ID
- */
- private void setProgressDrawable(int resId) {
- Drawable drawable = MainActivity.this.getResources().getDrawable(resId);
- drawable.setBounds(pBar.getProgressDrawable().getBounds());
- /* 设置drawable之后貌似getProgress是从当前的为progress值为起始,
- * 所以注意先取得progress,然后重置progres=0,然后设置drawable,
- * 最后再把原来的progress还原,要不然设置了drawable之后ProgressBar的前景色会消失
- * 这里不会出现这种问题,因为progress始终是变化的,前景色的变化设置drawable也只执行一次
- */
- pBar.setProgressDrawable(drawable);
- }
- /**
- * 电池滚动条特效
- */
- private TimerTask task = new TimerTask() {
- int add = 0;
- public void run() {
- try {
- while (add++ < progressAdd) {
- int progress = pBar.getProgress() + actionType;
- progressOld = progress - actionType;
- if (progress <= BATTERYWARN) {
- // 初始时 或者 原先的值大于下限(只执行一次动态改变ProgressDrawable)
- if (progressOld == 0 || progressOld > BATTERYWARN) {
- setProgressDrawable(R.drawable.main_progress_vertical_warn);
- }
- } else {
- // 原先的本身比下限小并且有值则还原进度条默认颜色(只执行一次动态改变ProgressDrawable,原来没值时按默认)
- if (progressOld > 0 && progressOld <= BATTERYWARN) {
- setProgressDrawable(R.drawable.main_progress_vertical);
- }
- }
- pBar.setProgress(progress);
- // 最后一次不用休眠
- if (add < progressAdd) {
- Thread.sleep(30);
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- };
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- bindData(true);
- }
- /**
- * 初始化view
- */
- private void initView() {
- pBar = (ProgressBar) findViewById(R.id.pb_battery);
- pBar.setMax(100);
- }
- /**
- * 绑定数据
- *
- * @param isStart
- * 初始绑定还是有广播数据变更需要刷新数据,tru初始绑定数据,false有广播数据变更需要刷新数据
- */
- private void bindData(boolean isStart) {
- // 测试数据,收入
- int moneyIn = 30;
- // 测试数据,预算
- int moneyYs = 300;
- // 测试数据,支出
- double moneyOut = 110;
- // 目前滚动条状态%值
- int progress = 100 - (int) ((moneyOut / (moneyIn + moneyYs)) * 100);
- /*测试下降
- //测试数据,原滚动条状态值
- pBar.setProgress(progress);
- //测试数据,别处操作了收入、支出、预算的数据使其发生了变化
- isStart = false;
- */
- // 初始加载
- if (isStart) {
- progressAdd = progress;
- } else {
- // 测试数据,测试下降需要把上面的 /*测试下降 注释去掉
- progress = 5;
- progressOld = pBar.getProgress();
- // 上升还是下降
- progressAdd = progress - progressOld;
- if (progressAdd < 0) {
- actionType = -1;
- progressAdd *= -1;
- }
- }
- // 过500毫秒执行task
- timer.schedule(task, 500);
- }
- }
0 0
- android 垂直的进度条实现
- 垂直进度条的实现
- VerticalProgressBar 垂直进度条的实现
- 垂直进度条VerticalProgressBar的实现
- Android---垂直的进度条(VerticalSeekBar、VerticalProgressBar)
- 实现垂直进度条
- 如何实现垂直进度条
- android 垂直方向进度条progressbar
- Android垂直ProgressBar的实现
- android 下载进度条的实现
- android:圆形进度条的实现
- android 自定义进度条的实现
- Android实现垂直型的SeekBar
- Android实现垂直型的SeekBar
- Android垂直ViewPager效果的简易实现
- Android文件下载进度条的实现
- Android文件下载进度条的实现
- Android文件下载进度条的实现
- 中国国家气象局天气预报信息接口
- 通过TabActivity创建底部菜单栏
- HTML5边玩边学(5):图像、图案和字体 一、创建图像对象
- Spring <aop:config> 疑问 -- 完全由spring代理对象才能被事务管理
- Android系统自带样式(android:theme)(转)
- android 垂直的进度条实现
- POJ-1200(N进制字符串hash)
- 函数后面加const
- 利用Fragment实现Tab页
- 面过的那些题
- 求最小的前n个数
- ORACLE 10G imp error: ORA-12899
- poj 3041 Asteroids
- mainActivity