设置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(); } } }
相关文章推荐
- android控件的隐藏与显示
- Android ViewGroup事件分发机制总结
- Mac下设置Android源代码编译环境
- 如何设置Android软键盘的默认不弹出?
- 编译Android AOSP代码
- Android编译过程中的碎碎念
- Android学习笔记----反编译APK文件
- Android学习笔记----常用adb指令
- android 密码显示与隐藏的切换
- Android悬浮缩放菜单的实现
- Android实现数据存储技术
- I.MX6 Android 移除 Settings wifi功能
- 【Android】19.1 SharedPreferences类
- 【Android】19.0 第19章 前面章节的代码优化及本章示例主界面
- android的5种布局方式
- Android ndk中进行log的方法
- Android Studio 使用GitHub
- Android学习笔记----数据持久存储
- ubuntu下载编译android5.0
- android应用程序的安装方式与原理