<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:orientation="vertical" > <Button android:id="@+id/btn_download" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="点击下载" /> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/iv_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitCenter" /> </FrameLayout> </LinearLayout>
package com.example.asynctask;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URLConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener{
private ImageView image;
private ProgressDialog progress;
private Button btn_download;
private static String URL="http://img4.imgtn.bdimg.com/it/u=1256159061,743487979&fm=21&gp=0.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView) findViewById(R.id.iv_image);
btn_download=(Button) findViewById(R.id.btn_download);
progress=new ProgressDialog(this);
progress.setIcon(R.drawable.ic_launcher);
progress.setTitle("提示信息");
progress.setMessage("正在下载,请稍候...");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
btn_download.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MyAsyncTask().execute(URL);
}
/*
* String*********对应我们的URL类型
* Integer********进度条的进度值
* BitMap*********异步任务完成后返回的类型
* */
class MyAsyncTask extends AsyncTask<String, Integer, Bitmap>{
//执行异步任务(doInBackground)之前执行,并且在ui线程中执行
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
if(image!=null){
image.setVisibility(View.GONE);
}
//开始下载 对话框进度条显示
progress.show();
progress.setProgress(0);
}
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
//params是一个可变长的数组 在这里我们只传进来了一个url
String url=params[0];
Bitmap bitmap=null;
URLConnection connection;
InputStream is;//用于获取数据的输入流
ByteArrayOutputStream bos;//可以捕获内存缓冲区的数据,转换成字节数组。
int len;
float count=0,total;//count为图片已经下载的大小 total为总大小
try {
//获取网络连接对象
connection=(URLConnection) new java.net.URL(url).openConnection();
//获取当前页面的总长度
total=(int)connection.getContentLength();
//获取输入流
is=connection.getInputStream();
bos=new ByteArrayOutputStream();
byte []data=new byte[1024];
while((len=is.read(data))!=-1){
count+=len;
bos.write(data,0,len);
//调用publishProgress公布进度,最后onProgressUpdate方法将被执行
publishProgress((int)(count/total*100));
//为了显示出进度 人为休眠0.5秒
Thread.sleep(500);
}
bitmap=BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.toByteArray().length);
is.close();
bos.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
//在ui线程中执行 可以操作ui
@Override
protected void onPostExecute(Bitmap bitmap) {
// TODO Auto-generated method stub
super.onPostExecute(bitmap);
//下载完成 对话框进度条隐藏
progress.cancel();
image.setImageBitmap(bitmap);
image.setVisibility(View.VISIBLE);
}
/*
* 在doInBackground方法中已经调用publishProgress方法 更新任务的执行进度后
* 调用这个方法 实现进度条的更新
* */
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
progress.setProgress(values[0]);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有