您的位置:首页 > 移动开发 > Android开发

设置Android的ProgressBar的实时进度及比例

2016-03-05 11:51 288 查看
//这是文件的布局
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.mobiletrain.asynctaskimage.MainActivity" >

<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="256dp" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv"
android:onClick="btnClick"
android:text="下载图片" />

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn" />

<ProgressBar
android:id="@+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv"
android:max="100" />

</RelativeLayout>

//源码
package com.example.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnClick(View v){
DownData dd =  new DownData();
dd.execute("http://img6.faloo.com/Picture/0x0/0/183/183378.jpg");
}

class DownData extends AsyncTask<String, Integer, byte[]>{
private ProgressBar pro ;
private TextView Text;

@Override
protected void onPreExecute() {

Text= (TextView) findViewById(R.id.tv);
pro= (ProgressBar) findViewById(R.id.pb);

}

@Override
protected void onPostExecute(byte[] result) {
Bitmap bitmap = BitmapFactory.decodeByteArray(result,0,result.length);
ImageView img= (ImageView) findViewById(R.id.iv);
img.setImageBitmap(bitmap);
}

@Override
protected void onProgressUpdate(Integer... values) {
if(values.length>0){
Text.setText(values[0]+ "%");
pro.setProgress(values[0]);
}
}

@Override
protected byte[] doInBackground(String... params) {
// TODO Auto-generated method stub
HttpURLConnection con = null;
InputStream is = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
URL url= new URL(params[0]);
con=(HttpURLConnection) url.openConnection();
con.setConnectTimeout(5*1000);
//获得总字节数
double totalnum = con.getContentLength();
//设置当前的总字节数
int count = 0;
con.connect();
//开始解析
if(con.getResponseCode()==200){
is= con.getInputStream();
int len =0;
byte [] bytes = new byte [1024*5];
while((len=is.read(bytes))!=-1){
baos.write(bytes,0,len);
baos.flush();
count +=len;
publishProgress((int)(count /totalnum)*100);

}

}

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{

if (con != null) {
con.disconnect();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

return baos.toByteArray();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: