当前位置: 代码迷 >> Android >> 稳扎稳打_Android开发课[23]_用户界面之ProgressBar(进度条)
  详细解决方案

稳扎稳打_Android开发课[23]_用户界面之ProgressBar(进度条)

热度:476   发布时间:2016-04-28 01:33:22.0
步步为营_Android开发课[23]_用户界面之ProgressBar(进度条)

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主题:用户界面之ProgressBar(进度条)
    -
    自定义ProgressBar进度条(实例):

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical">      <Button          android:id="@+id/button"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="开启ProgressBar"    />      <ProgressBar          android:id="@+id/progressBar1"          style="?android:attr/progressBarStyleLarge"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:progress="0"        android:visibility="gone" />    <ProgressBar          android:id="@+id/progressBar2"          android:layout_width="fill_parent"           android:layout_height="10dip"          android:max="100"          android:progress="0"         android:secondaryProgress="0"         style="?android:attr/progressBarStyleHorizontal"           android:progressDrawable="@drawable/progressbar_layer_list"           android:background="@drawable/progressbar_box"         android:visibility="gone"      />  </LinearLayout>  

在res文件下创建drawable文件,再在drawable文件下创建progressbar_layer_list.xml。
progressbar_layer_list.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">        <clip>            <shape>                <corners android:radius="15dip" />                <gradient android:startColor="#00b7ee" android:endColor="#0075a9" android:angle="270" android:centerY="0.75"/>            </shape>        </clip>    </item>    <item android:id="@android:id/secondaryProgress">        <clip>            <shape>                <corners android:radius="15dip" />                <gradient android:startColor="#00b7ee" android:endColor="#0075a9" android:angle="270" android:centerY="0.75"/>            </shape>        </clip>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <gradient android:startColor="#00b7ee" android:endColor="#0075a9" android:angle="270" android:centerY="0.25"/>                <corners android:radius="15dip"/>            </shape>        </clip>    </item></layer-list>

progressbar_box图片资源如下:

这里写图片描述

MainActivity.java源代码:

import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;    public class MainActivity extends Activity {    private ProgressBar pro1,pro2;    private Button button;    private int count;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button)findViewById(R.id.button);        pro1 = (ProgressBar)findViewById(R.id.progressBar1);        pro2 = (ProgressBar)findViewById(R.id.progressBar2);     // 设置进度条是否自动运转,false表是不自动运转        pro1.setIndeterminate(false);        pro2.setIndeterminate(false);        button.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View arg0) {                // 设置ProgressBar为可见状态                pro1.setVisibility(View.VISIBLE);                pro2.setVisibility(View.VISIBLE);                // TODO Auto-generated method stub                new Thread(new Runnable(){                    public void run() {                        for(int i=0; i<10; i++){                            try{                                count = i*10;                                Thread.sleep(1000);                                if(count >= 100){                                    Message msg = new Message();                                    msg.what = 1;                                    MainActivity.this.myHandler.sendMessage(msg);                                }else{                                    Message msg = new Message();                                    msg.what = 2;                                    MainActivity.this.myHandler.sendMessage(msg);                                }                            }catch(Exception e){                                e.printStackTrace();                            }                        }                    }                }).start();            }        });    }        Handler myHandler = new Handler(){        public void handleMessage(Message msg){            switch(msg.what){            case 1:                Thread.currentThread().interrupt();                break;            case 2:                if (!Thread.currentThread().isInterrupted()) {                    // 改变ProgressBar的当前值                    pro1.setProgress(count);                    pro2.setProgress(count);                    // 设置标题栏中前景的一个进度条进度值                    //setProgress(count * 100);                    // 设置标题栏中后面的一个进度条进度值                    //setSecondaryProgress(count * 100);                }                break;            }            super.handleMessage(msg);        }    };  }  

运行结果:

这里写图片描述

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305

  相关解决方案