【安卓笔记】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) { // 代码在上面,这里就不写了 } }显示效果:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。