【安卓笔记】progressbar使用技巧

当在加载数据到litView或者其他控件上时,用户会有一个等待的时间,为了给用户一个友好的提示,通常我们会显示一个ProgressBar,待数据加载完毕后,ProgressBar自动消失。
通常的做法是在布局外面套一个FrameLayout,然后在FramLayout下有两个子节点,一个作为显示的内容(如listView)另一个是ProgressBar,但是这样做会导致activity布局嵌套过深,不建议这样做。
其实我们可以通过代码动态添加一个ProgressBar。

代码如下:
/**
     * 动态创建ProgressBar
     * @param a
     * @return
     */
    public ProgressBar createProgressBar(Activity a)
    {
//        1.找到activity根部的ViewGroup,类型都为FrameLayout。
        FrameLayout rootContainer = (FrameLayout) a.findViewById(android.R.id.content);//固定写法,返回根视图
//        2.初始化控件显示的位置
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        lp.gravity = Gravity.CENTER;
//        3.设置控件显示位置
        ProgressBar pb = new ProgressBar(a);
        pb.setLayoutParams(lp);
        pb.setVisibility(View.GONE);//默认不显示
//        4.将控件加到根节点下
        rootContainer.addView(pb);
        return pb;
    }
小案例:下载网络图片:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1000" />
    <Button
        android:id="@+id/but_down"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下载图片" />
</LinearLayout>
activity:
package com.example.asynctaskdemo1;

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{
    private static final String PATH = "http://h.hiphotos.baidu.com/image/w%3D2048/sign=d78504fa9d3df8dca63d8891f929738b/9f510fb30f2442a77e79f7a8d343ad4bd1130243.jpg";
    private Button but_down = null;
    private ImageView iv_show = null;
    private ProgressBar pb = null;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        but_down = (Button) findViewById(R.id.but_down);
        iv_show = (ImageView) findViewById(R.id.iv_show);
        but_down.setOnClickListener(this);
        
        pb = this.createProgressBar(this);
    }
    @Override
    public void onClick(View v)
    {
        if(v.getId() == R.id.but_down)
        {
            new DownloadTask().execute(PATH);
        }
    }
    
    private class DownloadTask extends AsyncTask<String,Void,Bitmap>
    {
        @Override
        protected void onPreExecute()
        {
            pb.setVisibility(View.VISIBLE);
        }
        @Override
        protected Bitmap doInBackground(String... params)
        {
            String path = params[0];
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(path);
            try
            {
                HttpResponse resp = client.execute(get);
                if(resp.getStatusLine().getStatusCode() == 200)
                {
                    HttpEntity entity = resp.getEntity();
                    if(entity!=null)
                    {
                        InputStream is = entity.getContent();
                        Options opts = new Options();
                        opts.inSampleSize = 2;
                        Bitmap bitmap = BitmapFactory.decodeStream(is,null,opts);
                        is.close();
                        return bitmap;
                    }
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Bitmap result)
        {
            pb.setVisibility(View.GONE);
            if(result!=null)
            {
                iv_show.setImageBitmap(result);
            }else
            {
                Toast.makeText(MainActivity.this,"下载失败..", 0).show();
            }
        }
    }
    
    /**
     * 动态创建ProgressBar
     * @param a
     * @return
     */
    public ProgressBar createProgressBar(Activity a)
    {
//        代码在上面,这里就不写了
    }
}
显示效果:
【安卓笔记】progressbar使用技巧【安卓笔记】progressbar使用技巧

【安卓笔记】progressbar使用技巧

上一篇:Android--SQLite(增,删,改,查)操作


下一篇:Android天气应用“SimpleWeather”