加载图片属于比较耗时的工作,我们需要异步进行加载,异步加载有两种方式:1.通过AsyncTask类进行;2.通过Handler来实现,下面我们就来看一下如何通过这两种方式实现网络图片的异步加载。
一、AsyncTask方式
1.main.xml:
<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" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
2.MainActivity.java:
package com.example.imageloaderdemo;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView mImageView;
private static String URLSTRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.img);
URLSTRING="http://photocdn.sohu.com/20110927/Img320705637.jpg";//图片地址
MyAsyncTask myAsyncTask=new MyAsyncTask();
myAsyncTask.execute(URLSTRING);
}
class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
mImageView.setImageBitmap(result);
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap=null;
try {
URL url=new URL(params[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
bitmap=BitmapFactory.decodeStream(inputStream);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
}
3.总结:
AsyncTask<String, Void, Bitmap>//三个参数,第一个为传入的URL,第二个表示中间状态的,这里传入空即可,第三个为返回值
doInBackground(String... params)方法新开了一个线程,AysncTask其他几个方法都在主线程中运行
mImageView.setImageBitmap(result);可以直接设置
运行实例如下:
成功的加载了网络图片。
二、Handler方式
异步请求Bitmap,因为子线程无法更新主线程UI,所以必须通过Handler的方式进行图片渲染。
布局文件不变,下面我们看一下MainActivity.java:
package com.example.imageloaderdemo;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView mImageView;
private static String URLSTRING;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap) msg.obj;
mImageView.setImageBitmap(bitmap);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.img);
URLSTRING = "http://photocdn.sohu.com/20110927/Img320705637.jpg";// 图片地址
MyThread myThread = new MyThread();
myThread.start();// 调用线程
}
class MyThread extends Thread {
@Override
public void run() {
super.run();
Bitmap bitmap = null;
try {
URL url = new URL(URLSTRING);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
if (connection.getResponseCode() == 200) {
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
Message message = new Message();
message.obj = bitmap;
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
总结:
class MyThread extends Thread{}
2.通过sendMessage发送对象,通过handleMessage处理数据。
很简单啊,希望可以帮到初学者。
喜欢的朋友关注我!谢谢
标签:异步,java,第四十九,Bitmap,connection,import,Android,bitmap,android From: https://blog.51cto.com/u_15866446/5843748