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

Android 圖片顯示元件 ImageView 詳解

編輯:Android 程式設計入門

ImageView 是 Android 中最基礎的圖片顯示元件,掌握它才能真正做好介面呈現。本文將說明它的基本概念、關鍵屬性,並透過實作釐清 src 與 background 的差異。

一、什麼是 ImageView

顧名思義,ImageView 就是用來在介面上顯示圖片的控制元件,使用極為廣泛。

二、常用屬性

android:src 設定 ImageView 的內容圖片
android:background 設定 ImageView 的背景圖片
android:color 設定 ImageView 的 RGB 顏色

三、src 與 background 的區別

為了清楚展示兩者的差異,筆者在專案中新增了一張名為 ic_lena 的圖片。這個範例的目的正是要說明 src 與 background 的不同,同時也證明這兩個屬性可以並存。以下為 ImageView 部分的程式片段:

<ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_lena" android:background="#00FFFF" />

完整的 fragment_main.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_lena" android:background="#00FFFF" /> <ImageView android:id="@+id/imageView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/ic_lena" /> <ImageView android:id="@+id/imageView3" android:layout_width="match_parent" android:layout_height="30dp" android:background="#00FFFF" /> <ImageView android:id="@+id/imageView3" android:layout_width="200dp" android:layout_height="200dp" android:background="@drawable/ic_lena" /> </LinearLayout>

不同解析度下圖片的顯示:

ic_lena 圖片:

運行效果:

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