Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2621707
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Android平台

2013-10-27 22:59:32

    正好看到进度条章节,就顺便配合下“线程队列”来试试效果啥的,也行!程序有毛病的,长按一次效果行,之后不能长按了,否则线程会一直跑,嘿嘿!
    布局文件: acitivity_ui.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >

  6.     <ProgressBar
  7.         android:id="@+id/progress1"
  8.         style="?android:attr/progressBarStyleHorizontal"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:max="100"
  12.         android:progress="0"
  13.         android:progressDrawable="@drawable/back_color" />

  14.     <Button
  15.         android:id="@+id/button_progress1"
  16.         android:layout_width="wrap_content"
  17.         android:layout_height="wrap_content"
  18.         android:text="显示进度条" />

  19. </LinearLayout>
    进度体文件: back_color.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <layer-list xmlns:android="" >

  3.     <!-- 设置背景色(黑色) -->

  4.     <item android:id="@android:id/background">
  5.         <shape>
  6.             <corners android:radius="30dip" />

  7.             <gradient
  8.                 android:endColor="#000000"
  9.                 android:startColor="#000000" />
  10.         </shape>
  11.     </item>

  12.     <!-- 设置进度条颜色(白色) -->

  13.     <item android:id="@android:id/progress">
  14.         <clip>
  15.             <shape>
  16.                 <corners android:radius="30dip" />

  17.                 <gradient
  18.                     android:endColor="#0fffff"
  19.                     android:startColor="#0fffff" />
  20.             </shape>
  21.         </clip>
  22.     </item>

  23. </layer-list>
    Activity源代码:

点击(此处)折叠或打开

  1. package com.example.control;

  2. import android.os.Bundle;
  3. import android.os.Handler;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.ProgressBar;

  9. public class Ui extends Activity {
  10.     private Button btn_pro1;
  11.     private ProgressBar progress1;
  12.     private Handler handler;
  13.     private Runnable runnable;
  14.     private int counter = 0;

  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_ui);

  19.         progress1 = (ProgressBar)findViewById(R.id.progress1);
  20.         btn_pro1=(Button)findViewById(R.id.button_progress1);
  21.         btn_pro1.setOnClickListener(new View.OnClickListener() {
  22.             @Override
  23.             public void onClick(View v) {
  24.                 // TODO Auto-generated method stub
  25.                 setProgressBarIndeterminateVisibility(true);
  26.                 progress1.incrementProgressBy(1);
  27.                 counter += 1;
  28.             }
  29.         });
  30.         
  31.         ///< 创建队列
  32.         handler = new Handler();
  33.         ///< 创建runnable对象,类似创建线程
  34.         runnable = new Runnable () {
  35.             public void run () {
  36.                 progress1.incrementProgressBy(1);
  37.                 counter += 1;
  38.                 if (30 == counter)
  39.                 {
  40.                     handler.removeCallbacks(this);         ///< 从线程队列中删除
  41.                     return;
  42.                 }
  43.                 handler.postDelayed(this, 500);         ///< 再次添加到线程队列
  44.             }
  45.         };

  46.         btn_pro1.setOnLongClickListener(new View.OnLongClickListener() {

  47.             @Override
  48.             public boolean onLongClick(View v) {
  49.                 // TODO Auto-generated method stub
  50.                 handler.postDelayed(runnable, 500);     ///< 延时一定时间后向线程中添加队列
  51.                 return false;
  52.             }
  53.         });
  54.     }

  55.     @Override
  56.     public boolean onCreateOptionsMenu(Menu menu) {
  57.         // Inflate the menu; this adds items to the action bar if it is present.
  58.         getMenuInflater().inflate(R.menu.ui, menu);
  59.         return true;
  60.     }

  61. }
    样子:
    


阅读(6561) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~