編輯:關於 Android 程式設計
ImageView 是最基礎的圖像顯示控件,除了展示圖片,還支持放大、縮小、旋轉等操作。本篇先把它的幾個核心屬性掰扯清楚。

先寫四個控件做對比實驗:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/maomi" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:src="@drawable/maomi" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/maomi" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="@drawable/maomi" />
</LinearLayout>
運行效果:


規律很明顯:src 會按圖片本身的比例去適應容器,容器寬度被限制時圖片會等比縮小;而 background 則是無條件把圖片填滿整個容器,比例一概不管。
還有一點值得注意:如果同時指定了 width 和 height,background 依然老老實實填充,而 src 的尺寸則可能發生變化。

這就引出了 ImageView 的一個重要屬性——scaleType,也就是圖片縮放策略。這個內容比較多,放到後面細說。
上面的第四個效果圖裡,正方形已經被拽成了長方形,顯然很難看。目前常見的解法有兩種:
方法一:把尺寸寫死。動態添加 ImageView 時直接指定固定大小即可。

方法二:用 BitmapDrawable 的 XML 包裝一層,指定 gravity。

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/cat_bg"
android:gravity="top"
android:src="@drawable/maomi"
android:tileMode="disabled" />
使用時,動態方式:ibtnCat.setBackgroundResource(R.drawable.cat);;靜態方式:android:background="@drawable/cat"。
補充:透明度設置。

網上見過一張做法示意圖:


效果如下:

實現非常簡單——background 鋪一個純色,src 放圖片:
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#CCAADF"
android:src="@drawable/maomi" />

兩者也可以在代碼中分別設置:


運行效果:

<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"
tools:context="com.example.android_drawable_xml.MainActivity">
<!-- 正常顯示 -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:src="@drawable/maomi" />
<!-- 限制最大寬高,並開啟邊界調整以保持長寬比 -->
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:adjustViewBounds="true"
android:maxHeight="200px"
android:maxWidth="200px"
android:src="@drawable/maomi" />
</LinearLayout>

加上 adjustViewBounds="true" 之後,即使限制了 max 尺寸,圖片也不會被拉變形。


一幅圖看懂它們幾個:


下面逐一做實驗。
三者都是「等比縮放至完全顯示」,區別只在對齊位置。以 fitEnd 為例:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageview"
android:layout_width="300px"
android:layout_height="300px"
android:layout_margin="5px"
android:background="#AAFD00"
android:scaleType="fitEnd"
android:src="@drawable/mengchong" />
</LinearLayout>

兩者都居中,差別在於:centerCrop 撐滿容器並裁掉溢出部分;centerInside 則在圖片大於容器時縮小到能完整顯示。

<ImageView
android:id="@+id/imageview2"
android:layout_width="300px"
android:layout_height="300px"
android:layout_margin="5px"
android:background="#AAFD00"
android:scaleType="centerCrop"
android:src="@drawable/mengchong" />
<ImageView
android:id="@+id/imageview3"
android:layout_width="300px"
android:layout_height="300px"
android:layout_margin="5px"
android:background="#AAFD00"
android:scaleType="centerInside"
android:src="@drawable/mengchong" />

不保持比例,直接把圖片拉伸到容器大小:
<ImageView
android:id="@+id/imageview4"
android:layout_width="300px"
android:layout_height="300px"
android:layout_margin="5px"
android:background="#AAFD00"
android:scaleType="fitXY"
android:src="@drawable/mengchong" />
運行效果:

我選的圖比較小,所以看不出變化;換成一張尺寸較大的圖,就會明顯看到它被拉成「高瘦」或「矮胖」。
基於矩陣變換,不做任何自動縮放,是繪圖控件的常客:
<ImageView
android:id="@+id/imageview"
android:layout_width="300px"
android:layout_height="300px"
android:layout_margin="5px"
android:background="#AAFD00"
android:scaleType="matrix"
android:src="@drawable/mengchong" />

運行效果:

保持原尺寸居中顯示,不放大也不縮小:
<ImageView
android:id="@+id/imageview"
android:layout_width="100px"
android:layout_height="100px"
android:layout_margin="5px"
android:background="#AAFD00"
android:scaleType="center"
android:src="@drawable/meizi" />

運行效果:

以上就是 ImageView 較基礎的一部分內容,下篇進入實戰應用。
Android 自訂數字鍵盤:高仿微信支付密碼鍵盤實作教學
現在很多 App 的付款、輸入密碼環節,都換成了自訂的數字鍵盤。除了更好控制輸入行為,視覺上也確實比系統鍵盤精緻得多。 下面帶大家一起做一個高仿微信的數字鍵盤,完成後可以
Android ListView 四種 Adapter 用法對比與選型指南
ListView 是 Android 開發中使用頻率極高的 UI 控件。它的每一個子項既可以是一行簡單的字符串,也可以是一組 View 的複合布局,展示形式完全由開發者定
Android 開發筆記:用 AsyncTask 優雅地完成網路請求與 UI 更新
在前面的文章裡,我們已經把 Android 上常見的網路請求方式梳理過一遍: 一、兩大類請求方式回顧 1. 基於 TCP/URL 的編程 ServerSocket、Soc
手機QQ群簽到在哪裡?連續簽到的獎勵與操作路徑
群簽到是QQ群裡一個容易被忽略的小功能,但獎勵還算實際:連續 15 天可獲得群空間橙名特權,連續 30 天則可換到會員試用。代價只是每天點兩下。簽到能得到什麼・連續 15