加载图片属于比较耗时的工作,我们需要异步进行加载,异步加载有两种方式: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.总结:
1.AsyncTask<String, Void, Bitmap>//三个参数,第一个为传入的URL,第二个表示中间状态的,这里传入空即可,第三个为返回值
2.doInBackground(String... params)方法新开了一个线程,AysncTask其他几个方法都在主线程中运行
3.除了doInBackground方法,其他方法都在主线程中运行,所以这里:
- mImageView.setImageBitmap(result);可以直接设置
运行实例如下:
![](https://i-blog.csdnimg.cn/blog_migrate/91c8819f20a14bdbb8b4da944860b187.png)
成功的加载了网络图片。
二、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();
- }
- }
- }
- }
总结:
1.新开一个线程用于网络数据的请求:
- class MyThread extends Thread{}
2.通过sendMessage发送对象,通过handleMessage处理数据。
很简单啊,希望可以帮到初学者。
喜欢的朋友关注我!谢谢