為了實現「設定開機畫面」的圖片選擇功能,我們選擇 Gallery 作為基礎元件,並加入 3D 旋轉切換與圖片倒影效果,打造視覺豐富的選圖體驗。以下為完成效果:


Gallery 本身不支援 3D 變換,需自訂 CoverFlowGallery 繼承 Gallery,並覆寫兩個核心方法:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// 重新計算 CoverFlow 的幾何中心,供後續旋轉角度計算使用
mCoveflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
當 Gallery 大小改變(如螢幕旋轉、鍵盤彈出)時,必須重新計算中心點,確保 3D 效果幾何正確。
@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
final int childCenter = getCenterOfView(child);
final int childWidth = child.getWidth();
int rotationAngle = 0;
t.clear();
t.setTransformationType(Transformation.TYPE_BOTH); // 同時套用 alpha 與 matrix
if (childCenter == mCoveflowCenter) {
// 正中央項目:無旋轉,完整顯示
transformImageBitmap((ImageView) child, t, 0);
} else {
// 側邊項目:根據偏離中心的距離計算旋轉角度
rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
rotationAngle = Math.max(-mMaxRotationAngle, Math.min(mMaxRotationAngle, rotationAngle));
transformImageBitmap((ImageView) child, t, rotationAngle);
}
return true; // 表示已處理變換
}
關鍵邏輯:mMaxRotationAngle 定義最大旋轉角度(如 45°)。偏離中心越遠,旋轉越大,形成立體覆蓋流視覺。transformImageBitmap() 內部對 ImageView 套用 Matrix 旋轉與縮放。
為了呈現「倒影」效果,Adapter 在 getView() 中即時合成 Bitmap:原圖 + 間距 + 垂直翻轉並漸層透明的倒影圖。
private boolean createReflectedImages() {
final int reflectionGap = 4;
final int targetHeight = 200;
int index = 0;
for (Map<String, Object> map : dataList) {
Integer resId = (Integer) map.get("image");
Bitmap original = BitmapFactory.decodeResource(context.getResources(), resId);
// 1. 等比縮放至目標高度
float scale = targetHeight / (float) original.getHeight();
Matrix scaleMatrix = new Matrix();
scaleMatrix.postScale(scale, scale);
Bitmap scaled = Bitmap.createBitmap(original, 0, 0,
original.getWidth(), original.getHeight(), scaleMatrix, true);
original.recycle(); // 釋放原圖記憶體
// 2. 建立倒影:取下半部、垂直翻轉
Matrix flipMatrix = new Matrix();
flipMatrix.preScale(1, -1); // Y 軸鏡像
Bitmap reflection = Bitmap.createBitmap(scaled, 0, scaled.getHeight() / 2,
scaled.getWidth(), scaled.getHeight() / 2, flipMatrix, false);
// 3. 合成最終圖:原圖 + 間距 + 倒影
Bitmap withReflection = Bitmap.createBitmap(scaled.getWidth(),
scaled.getHeight() + scaled.getHeight() / 2 + reflectionGap,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(withReflection);
canvas.drawBitmap(scaled, 0, 0, null);
canvas.drawRect(0, scaled.getHeight(), scaled.getWidth(),
scaled.getHeight() + reflectionGap, new Paint()); // 間距
canvas.drawBitmap(reflection, 0, scaled.getHeight() + reflectionGap, null);
// 4. 可選:對倒影套用漸層透明 (LinearGradient + PorterDuffXfermode)
// ...
// 存入快取供 getView 使用
reflectedBitmaps.put(index++, withReflection);
}
return true;
}
記憶體管理重點:
recycle() 釋放不再使用的 Bitmap。LruCache 快取合成後的 Bitmap,避免重複計算。Glide/Picasso 等圖片載入庫處理記憶體與快取。注意:Gallery 已在 API Level 16 (Android 4.1) 標示為 deprecated。新專案建議使用 RecyclerView + 自訂 LayoutManager 或第三方庫(如 CarouselLayoutManager、CoverFlowView)實現同樣效果,並獲得更好的效能、無障礙支援與維護性。
透過繼承 Gallery 覆寫 getChildStaticTransformation() 實現 3D 旋轉,並在 Adapter 中動態合成倒影 Bitmap,可快速打造視覺震撼的 3D 相簿。雖然 Gallery 已棄用,但其「子 View 靜態變換」的核心思想(Matrix + Transformation)在自訂 LayoutManager 或 Canvas 繪製中依然適用,值得深入理解。