Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

ImageView用法大全:加載本地與網絡圖片的三種方式

編輯:關於 Android 程式設計

在 Android 裡顯示圖片,最常用到的就是 ImageView。這裡把它的幾種基本用法整理一下,包含 XML 宣告、純代碼建立,以及加載網絡圖片。

一、直接在 XML 中使用

最簡單的方式,用 android:src 指定 drawable 資源即可:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" >

     <ImageView
         android:id="@+id/iv"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/welcome" />
 </LinearLayout> 

二、在代碼中動態建立

如果不想寫在佈局裡,也可以 new 一個出來再 setContentView:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     //setContentView(R.layout.activity_main);
     ImageView welcome = new ImageView(this);
     welcome.setImageResource(R.drawable.welcome);
     setContentView(welcome);
 }

構造 ImageView 時傳入的 this 代表當前的 Context。Context 由系統管理,負責資源解析、資料庫與偏好設定存取等工作。由於 Activity 本身就繼承自 Context,所以直接把 this 傳進去是最常見的做法。

三、加載網絡圖片

網絡圖片需要自己開連接、取串流、解碼成 Bitmap。下面這段把整個流程封裝成一個靜態方法:

 package com.android.antking.imageview;

 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;

 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.os.Bundle;

 public class MainActivity extends Activity {

     //定義一個圖片顯示控件
     private ImageView imageView;

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         //圖片資源
         String url = "/orignal/89429f6dhb99b4903ebcf&690";
         //得到可用的圖片
         Bitmap bitmap = getHttpBitmap(url);
         imageView = (ImageView) this.findViewById(R.id.imageViewId);
         //顯示
         imageView.setImageBitmap(bitmap);
     }

     /**
      * 獲取網絡圖片資源
      *
      * @param url 圖片地址
      * @return Bitmap 對象
      */
     public static Bitmap getHttpBitmap(String url) {
         URL myFileURL;
         Bitmap bitmap = null;
         try {
             myFileURL = new URL(url);
             //獲得連接
             HttpURLConnection conn = (HttpURLConnection) myFileURL.openConnection();
             //設置超時時間為 6000 毫秒
             conn.setConnectTimeout(6000);
             //獲得輸入流
             conn.setDoInput(true);
             //不使用緩存
             conn.setUseCaches(false);
             InputStream is = conn.getInputStream();
             //解析得到圖片
             bitmap = BitmapFactory.decodeStream(is);
             //關閉數據流
             is.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
         return bitmap;
     }
 }

這段寫法只是為了說明原理。真實專案裡,網絡請求必須放在子線程,主線程做網絡操作會直接拋 NetworkOnMainThreadException;同時也別忘了 ACCESS_NETWORK_STATE 與 INTERNET 權限。實務上更推薦直接用 Glide、Picasso 這類圖片加載框架,它們內建了線程管理、快取與生命週期綁定,能省掉大量樣板代碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved