演化理解 Android 非同步載入圖片

來源:互聯網
上載者:User

在學習"Android非同步載入映像小結"這篇文章時, 發現有些地方沒寫清楚,我就根據我的理解,把這篇文章的代碼重寫整理了一遍,下面就是我的整理。

下面測試使用的layout檔案:

簡單來說就是 LinearLayout 布局,其下放了5個ImageView。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:text="圖片地區開始" android:id="@+id/textView2"
  android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
 <ImageView android:id="@+id/imageView1"
  android:layout_height="wrap_content" android:src="@drawable/icon"
  android:layout_width="wrap_content"></ImageView>
 <ImageView android:id="@+id/imageView2"
  android:layout_height="wrap_content" android:src="@drawable/icon"
  android:layout_width="wrap_content"></ImageView>
 <ImageView android:id="@+id/imageView3"
  android:layout_height="wrap_content" android:src="@drawable/icon"
  android:layout_width="wrap_content"></ImageView>
 <ImageView android:id="@+id/imageView4"
  android:layout_height="wrap_content" android:src="@drawable/icon"
  android:layout_width="wrap_content"></ImageView>
 <ImageView android:id="@+id/imageView5"
  android:layout_height="wrap_content" android:src="@drawable/icon"
  android:layout_width="wrap_content"></ImageView>
 <TextView android:text="圖片地區結束" android:id="@+id/textView1"
  android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>我們將示範的邏輯是非同步從伺服器上下載5張不同圖片,依次放入這5個ImageView。上下2個TextView 是為了方便我們看是否阻塞了UI的顯示。

當然 AndroidManifest.xml 檔案中要配置好網路存取權限。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Handler+Runnable模式
我們先看一個並不是非同步線程載入的例子,使用 Handler+Runnable模式。

這裡為何不是新開線程的原因請參看這篇文章:Android Runnable 運行在那個線程 這裡的代碼其實是在UI 主線程中下載圖片的,而不是新開線程。

我們運行下面代碼時,會發現他其實是阻塞了整個介面的顯示,需要所有圖片都載入完成後,才能顯示介面。

package ghj1976.AndroidTest;

import java.io.IOException;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.widget.ImageView;

public class MainActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  loadImage("", R.id.imageView1);
  loadImage(",
    R.id.imageView2);
  loadImage(", R.id.imageView3);
  loadImage("",
    R.id.imageView4);
  loadImage("",
    R.id.imageView5);
 }

 private Handler handler = new Handler();

 private void loadImage(final String url, final int id) {
  handler.post(new Runnable() {
   public void run() {
    Drawable drawable = null;
    try {
     drawable = Drawable.createFromStream(
       new URL(url).openStream(), "image.gif");
    } catch (IOException e) {
     Log.d("test", e.getMessage());
    }
    if (drawable == null) {
     Log.d("test", "null drawable");
    } else {
     Log.d("test", "not null drawable");
    }
                                // 為了測試緩衝而類比的網路延時                                 SystemClock.sleep(2000);     ((ImageView) MainActivity.this.findViewById(id))
      .setImageDrawable(drawable);
   }
  });
 }
}

Handler+Thread+Message模式
這種模式使用了線程,所以可以看到非同步載入的效果。

核心代碼:

package ghj1976.AndroidTest;

import java.io.IOException;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.widget.ImageView;

public class MainActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  loadImage2("", R.id.imageView1);
  loadImage2("",
    R.id.imageView2);
  loadImage2("", R.id.imageView3);
  loadImage2("",
    R.id.imageView4);
  loadImage2("",
    R.id.imageView5);
 }

 final Handler handler2 = new Handler() {
  @Override
  public void handleMessage(Message msg) {
   ((ImageView) MainActivity.this.findViewById(msg.arg1))
     .setImageDrawable((Drawable) msg.obj);
  }
 };

 // 採用handler+Thread模式實現多線程非同步載入
 private void loadImage2(final String url, final int id) {
  Thread thread = new Thread() {
   @Override
   public void run() {
    Drawable drawable = null;
    try {
     drawable = Drawable.createFromStream(
       new URL(url).openStream(), "image.png");
    } catch (IOException e) {
     Log.d("test", e.getMessage());
    }

    // 類比網路延時
    SystemClock.sleep(2000);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.