編輯:關於 Android 開發
ImageView 看起來簡單,但 src 與 background 的行為差異、ScaleType 的八種取值,都是新手容易卡住的地方。這篇把這些細節整理清楚,最後再自訂一個圓形頭像。
顧名思義,background 是背景、src 是內容。實作上的關鍵差異在縮放行為:
src 填入圖片時,以圖片原始尺寸呈現,不自動拉伸。background 填入圖片時,會依 View 的指定尺寸縮放拉伸。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
<ImageView android:layout_width="200dp" android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<ImageView android:layout_width="200dp" android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
寬高都設 wrap_content 時,兩者看起來一樣大。但一旦你把寬度固定成 200dp,差異就很明顯:background 會把圖拉滿整個寬度,而 src 的圖片寬度不會變,此時圖片會自動居中,超出部分則被裁切——這就牽涉到縮放模式 ScaleType 了。
在 Java 端呼叫 setAlpha() 即可,參數是 0–255 的整數:
imageView.setAlpha(128);
要注意的是,這個方法只對 src 設定的圖片有效,對 background 無效。想調整背景透明度,得改用 backgroundAlpha 或 setImageAlpha()(API 21+)。
這個屬性單獨使用沒有效果,必須搭配 maxWidth 與 maxHeight 才有意義。它的作用是:當圖片需要被縮小時,按比例縮放而不是硬撐滿整個 View。
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/women" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:src="@mipmap/women"
android:adjustViewBounds="true"
android:maxHeight="200dp"
android:maxWidth="200dp"/>
也可以在 Java 端用 imageView.setScaleType(ImageView.ScaleType.CENTER) 設定。
實際做介面時,頭像通常用 centerCrop,產品圖常用 fitCenter,需要完整呈現的海報則用 fitXY 以外的選項避免變形。有興趣的話建議把八種都跑一遍,看過一次就不會再記錯。
做法是在 onDraw() 裡先用路徑畫一個圓,把它設為裁剪區域,再把圖畫進去:
public class RoundImageView extends ImageView {
private Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.women);
private Rect rect = new Rect();
private PaintFlagsDrawFilter filter =
new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG);
private Paint paint = new Paint();
private Path path = new Path();
public RoundImageView(Context context) { super(context); init(); }
public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); }
public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint.setStyle(Paint.Style.STROKE);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true); // 抗鋸齒,避免圓邊出現鋸齒
}
/** 外部可傳入一張 bitmap */
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bitmap == null) {
return;
}
rect.set(0, 0, getWidth(), getHeight());
canvas.save();
canvas.setDrawFilter(filter);
path.addCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, Path.Direction.CCW);
canvas.clipPath(path, Region.Op.REPLACE);
canvas.drawBitmap(bitmap, null, rect, paint);
canvas.restore();
}
}
版面檔中使用時,記得寬高要給等值,否則會被拉成橢圓:
<com.example.test3.RoundImageView
android:id="@+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"/>
這裡有兩個實務上的建議:一是圓心與半徑用 getWidth() 較保險(寬高相等時兩者一樣),二是別忘了 canvas.save() 與 canvas.restore() 成對出現,否則裁剪區域會污染到後續的繪製。若圓形頭像使用頻繁,也可以考慮用 CardView 或现成的 Glide Transformation,效能與維護性都更好。
Android 專案重構:目錄規劃與 Activity 生命週期整理
重新規劃 Android 專案結構 隨著專案規模變大,原本的目錄與程式碼組織容易變得混亂。重新規劃結構時,可以將與業務無關的邏輯抽到 AndroidLib 類庫,並將主專
Android 計算器 Pro 項目源碼解析
Android 計算器 Pro 項目源碼解析 這個計算器 Pro 專案主要示範 Android 應用中常見的數字輸入、運算符處理與結果顯示流程。雖然功能相對簡單,但很適合
Android Menu 選單實作:程式碼動態建立與 XML 宣告兩種寫法
選項選單(Options Menu)是 Android 早期版本最典型的操作入口。雖然後來被 Action Bar 與 Toolbar 的設計取代,但理解它的運作機制,仍
用懸浮窗進一步降低螢幕亮度、護眼過濾藍光(Android App 實作)
用懸浮窗進一步降低螢幕亮度、護眼過濾藍光(Android App 實作) 專案網址:https://github.com/hwding/make-it-darker 歡迎