上代码:
public class MainActivity extends Activity implements OnClickListener{
private EditText editText;
private Button button;
private ProgressBar progressBar;
private ImageView imageView;
private static final String URL_STRING="http://www.kekegold.com/d/file/yule/hqdy/2015-03-30/3099344f74b1bd6a1ba3622bf19ed8a1.png";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.editText1);
editText.setText(URL_STRING);
button=(Button) findViewById(R.id.button1);
progressBar=(ProgressBar) findViewById(R.id.progressBar1);
imageView=(ImageView) findViewById(R.id.imageView1);
button.setOnClickListener(this);
}
public class MyAsynaTask extends AsyncTask<String, Integer, Bitmap>{
private ImageView imageView;
private ProgressBar progressBar;
private Button button;
public MyAsynaTask( ImageView imageView,
ProgressBar progressBar,Button button) {
super();
this.imageView = imageView;
this.progressBar = progressBar;
this.button=button;
}
public MyAsynaTask( ImageView imageView,Button button) {
super();
this.imageView = imageView;
this.button=button;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
/*
* 改方法在执行实际的后台操作时被UI线程调用,可以在该方法中做一些准备工作,比如
* Toast.makeText(context, "准备下载", Toast.LENGTH_LONG).show();
*/
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params) {//输入编变长的可变参数 和UI线程中的Asyna.execute()对应
// TODO Auto-generated method stub
/*
* 该方法在OnpreExecute执行以后马上执行,改方法执行在后台线程当中,负责耗时的计算,可以调用publishProcess方法来实时更新任务进度
*/
Bitmap bitmap=null;
try {
URL url=new URL(params[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
// progressBar.setMax(connection.getContentLength());
InputStream inputStream=connection.getInputStream();
bitmap=BitmapFactory.decodeStream(inputStream);
// inputStream.close();
/*
* 以下完全为了演示进度条,如果为了显示进度条可以把这个去掉
*/
/*
byte []buf=new byte[1024*4];
inputStream=connection.getInputStream();
int len=0;
while ((inputStream.read(buf))!=-1) {
len+=buf.length;
publishProgress(len);
}
*/
inputStream.close();
} catch (Exception e) {
// TODO: handle exception
}
return bitmap;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
/*
* 当publichProcess 呗调用以后,UI线程将调用这个有方法在界面上展示任务的情况,比如一个额进度条。这里是更新进度条
*/
int value=values[0];
progressBar.setProgress(value);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
/*
* 在doInbackground执行完成以后,onPostExecute将被调用,后台的结果将返回给UI线程,将获得图片显示出来
*/
imageView.setImageBitmap(result);
button.setText("下载完成");
super.onPostExecute(result);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
MyAsynaTask mTask=new MyAsynaTask(imageView,button);
mTask.execute(URL_STRING);
break;
default:
break;
}
}
}
布局文件:
<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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_alignParentRight="true" />
<ProgressBar
android:layout_below="@+id/editText1"
android:id="@+id/progressBar1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="开始下载" />
<ImageView
android:layout_below="@+id/button1"
android:id="@+id/imageView1"
android:layout_width="400dp"
android:layout_height="300dp"
android:src="@drawable/ic_launcher"
/>
</RelativeLayout>
=============
加载一张图片效果很nice!
变一下,出现在listview场景中怎么用呢?
根据个人需求 比如我的是HorizontalListView
在HorizontalListViewAdapter中改到:
getView中改到:
MyAsynaTask mTask=new MyAsynaTask(holderView.imageView);
mTask.execute(mList.get(position).toString());
是不是感觉很happy呢?
我们搬的不是砖 是思想
另附 关于 缓存处理,下次再看吧
http://blog.csdn.net/zshshuai/article/details/7798086
感谢:http://www.linuxidc.com/Linux/2011-10/44388.htm
明天看看这个:http://blog.csdn.net/geniusxiaoyu/article/details/7470163