加载图片属于比较耗时的工作,我们需要异步进行加载,异步加载有两种方式:1.通过AsyncTask类进行;2.通过Handler来实现,下面我们就来看一下如何通过这两种方式实现网络图片的异步加载。
<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>
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; } } }
mImageView.setImageBitmap(result);可以直接设置
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{}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android实战简易教程-第四十九枪(两种方式实现网络图片异步加载)
原文地址:http://blog.csdn.net/yayun0516/article/details/48159415