步步为营_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

时间: 2024-08-04 06:04:36

步步为营_Android开发课[23]_用户界面之ProgressBar(进度条)的相关文章

步步为营_Android开发课[27]_用户界面之SeekBar(拖动条)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之SeekBar(拖动条) - SeekBar拖动条(实例): activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml

步步为营_Android开发课[19]_用户界面之ListView(列表视图)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之ListView(列表视图) -ListView和TextView,Button等控件一样,一样的定义方式和显示方式,所以不再重复学了.但是Android开发中ListView经常适配一些适配器来显示,所以我们要学重点是是ListView的各种适配. List

步步为营_Android开发课[14]_用户界面之Layout(布局)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之Layout(布局) -在Android开发中我们有传说中的5大布局,它们的结合使用,画出了APP界面的条条框框. Android中常用的5大布局: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件

步步为营_Android开发课[13]_用户界面之view(视图)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之view(视图) -View类是Android的一个超类,这个类几乎包含了所有的屏幕类型.每一个View都有一个用于绘图的画布,这个画布可以进行任意扩展.在游戏开发中叶可以自定义视图(View),这个画布的功能更能满足我们在游戏开发中的需要.在Android中

步步为营_Android开发课[30]_用户界面之ViewFlipper

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之ViewFlipper - ViewFlipper多视图切换(实例) 实现效果: 左右滑动,模拟欢迎界面的图片切换. activity_main.xml: <?xml version="1.0" encoding="utf-8&quo

步步为营_Android开发课[21]_用户界面之Notification(通知栏)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 本文资源借助于网友:幻影浪子 - 主题:用户界面之Notification(通知栏) - Notification实例(含系统自带Notification+半自定义Notification+自定义Notification) activity_main.xml: <?xml v

步步为营_Android开发课[20]_用户界面之Menu(菜单)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之Menu(菜单) - item元素的属性说明如下: - id:唯一标示菜单的ID引用. - menuCategory:菜单分类. - orderInCategory:分类排序. - title:菜单标题字符串. - titleCondensed:浓缩标题,适合

步步为营_Android开发课[17]_用户界面之Button(按钮)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之Button(按钮) Button和ImageButton控件实例: activity_main.xml源代码: <?xml version="1.0" encoding="utf-8"?> <LinearLay

步步为营_Android开发课[37]_用户界面之theme和style

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之theme和style -theme和style很容易混淆,theme样式是用于添加到application或者activity中去的.而style样式是用于添加到view控件中去的. 样式的定义: 找到项目文件夹:res/valus/styles,style