編輯:關於 Android 開發
ImageView 是 Android 中最基礎的圖片顯示元件。理解它的繼承關係與縮放模型,能省下大量「圖片怎麼就是怪怪的」的摸索時間。

ImageView 繼承自 View,主要功能是顯示圖片,也可顯示任何 Drawable 物件。
它的直接子類是 ImageButton 與 QuickContactBadge,間接子類包含 ZoomButton。因此——ImageView 的所有 XML 屬性與方法,基本上都能用在 ImageButton 與 ZoomButton 上。這解釋了為什麼 ImageButton 既能當按鈕又能設 scaleType。

android:adjustViewBounds:是否調整自身邊界以保持圖片的長寬比。需與 maxWidth、maxHeight 搭配使用,單獨設定無效。android:cropToPadding:設為 true 時,元件會被裁剪到保留 padding 的範圍。android:maxHeight / android:maxWidth:設定最大高與寬。單獨使用無效,需搭配 setAdjustViewBounds(true)。android:tint:把圖片渲染成指定顏色。android:src:指定要顯示的 Drawable 資源 ID。android:scaleType:決定圖片如何縮放或移動以適應元件大小。這是最常遇到的需求,需要三步一起設定:
android:adjustViewBounds="true";android:maxWidth 與 android:maxHeight;layout_width 與 layout_height 都設為 wrap_content。少了任何一步,比例就會被破壞。
ImageView.ScaleType 是一個內部列舉,透過 setScaleType() 設定。它是 ImageView 最容易搞錯的部分,以下逐一說明。

使用矩陣繪圖進行縮放,不做任何自動適配。這是自由度最高、也最需要自己計算的一種。
對圖片橫向、縱向獨立縮放,讓圖片完全填滿元件。圖片的長寬比可能會改變——這就是為什麼用這個模式時圖會被拉變形。
保持長寬比縮放,直到較長的邊與元件邊長相等,然後對齊到元件左上角。
同樣保持比例縮放,對齊到元件右下角。
同樣保持比例縮放,對齊到元件中央。這是最常用、也最安全的預設選擇。
圖片放在元件正中央,完全不縮放。若圖片比元件大,超出部分會被裁掉。
保持長寬比縮放,讓元件能完整顯示整張圖片。與 fitCenter 的差異在於:如果圖片本來就比元件小,它不會被放大。
保持長寬比,讓圖片完全覆蓋元件——只要圖片最短邊能對上即可,超出的部分被裁切。這正是「頭像填滿圓形框」類效果的標準做法。
// 使用 Bitmap 位圖設定 setImageBitmap(Bitmap bm); // 使用 Drawable 物件設定(注意:傳的是物件,不是資源 ID) setImageDrawable(Drawable drawable); // 使用圖片資源 ID 設定 setImageResource(int resId);
下面的例子做兩件事:切換顯示圖片,以及用滑鼠/觸控在圖片上「取局部特寫」。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/next" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="280dp"
android:scaleType="fitCenter"
android:src="@drawable/shuangta" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
package com.hust.imageviewtest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
// 圖片資源陣列
int[] images = new int[]{
R.drawable.lijiang, R.drawable.qiao, R.drawable.shuangta,
R.drawable.shui, R.drawable.xiangbi,
};
// 預設顯示的圖片索引
int currentImg = 2;
// 圖片初始透明度(0~255)
private int alpha = 255;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button plus = (Button) findViewById(R.id.button1);
final Button minus = (Button) findViewById(R.id.button2);
final Button next = (Button) findViewById(R.id.button3);
final ImageView image1 = (ImageView) findViewById(R.id.imageView1);
final ImageView image2 = (ImageView) findViewById(R.id.imageView2);
// 切換下一張圖片
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image1.setImageResource(images[++currentImg % images.length]);
}
});
// 調整透明度
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v == plus) alpha += 20;
if (v == minus) alpha -= 20;
if (alpha >= 255) alpha = 255;
if (alpha <= 0) alpha = 0;
image1.setAlpha(alpha);
}
};
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
// 觸控時在 image2 中顯示 image1 的局部特寫
image1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
// 位圖實際尺寸與顯示寬度(320)的縮放比
double scale = bitmap.getWidth() / 320.0;
int x = (int) (event.getX() * scale);
int y = (int) (event.getY() * scale);
// 邊界保護,避免取樣區域超出位圖範圍
if (x + 120 > bitmap.getWidth()) x = bitmap.getWidth() - 120;
if (y + 120 > bitmap.getHeight()) y = bitmap.getHeight() - 120;
image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
image2.setAlpha(alpha);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

縮放比換算是關鍵:ImageView 顯示時被縮小了,但 getBitmap() 拿到的是原始尺寸。觸控座標是「畫面上」的座標,必須乘上縮放比才能對應到「位圖中」的座標。少了這一步,取到的永遠是左上角那一塊。
邊界保護不可省:靠右下的觸控點加上 120×120 的取樣區域後很容易越界,若不夾住邊界會直接拋出 IllegalArgumentException。
回傳值決定事件是否續傳:onTouch 回傳 false,讓事件繼續往下傳遞,因此 ImageView 原本的點擊行為不受影響。
Android Shape 與 GradientDrawable 圖文詳解
概覽Android 可透過 XML 定義多種 Drawable,其中 shape 節點常用來建立 GradientDrawable。這類 drawable 可放在 res
Android 學習筆記:ListView 與相關適配器詳解
Android 第六天筆記——ListView 大解析 一、AdapterView AdapterView 繼承自 ViewGroup,本質上是一個容器。它派生了三個子類
用 100 多個 Styles 快速開發 Android 布局:一行搞定 View 屬性、一鍵統一配置 UI
用 100 多個 Styles 快速開發 Android 布局:一行搞定 View 屬性、一鍵統一配置 UI 在 Android 開發中,大量使用 XML 撰寫介面布局,
Android Shape 標籤完整整理:填色、漸層、描邊與圓角
Android Shape 標籤完整整理:填色、漸層、描邊與圓角 在 Android 開發中,很多時候我們並不需要一張切圖,只要一個 Shape 就能畫出按鈕底色、卡片