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

  1. //这是文件的布局
    <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();
    		}
    		
    	}
    
    }
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值