編輯:Android 資訊
ImageView 有 scaleType 屬性可以縮放圖片,讓它鋪滿屏幕寬度,但往往伴隨壓縮或裁剪的副作用。要徹底解決,得先弄清 scaleType 的每一種取值到底做了什麼。
scaleType 共有 matrix(默認)、center、centerCrop、centerInside、fitCenter、fitEnd、fitStart、fitXY 八種。
android:scaleType="center"
保持原圖尺寸,居中顯示在 ImageView 中。當原圖尺寸大於 ImageView,超出部分會被裁剪。
android:scaleType="centerCrop"
以填滿整個 ImageView 為目標,把原圖中心對齊 ImageView 中心,等比放大直到寬與高都填滿為止,超出部分作裁剪處理。
android:scaleType="centerInside"
以完整顯示原圖為目標,將圖片內容居中呈現,並按比例縮小到寬(高)小於或等於 ImageView 的寬(高)。如果原圖本身已經比 ImageView 小,則不做任何縮放,直接居中顯示。
android:scaleType="matrix"
不改變原圖尺寸,從 ImageView 左上角開始繪製,超出部分裁剪處理。
android:scaleType="fitCenter"
把原圖按比例放大或縮小到適配 ImageView,並居中顯示。
android:scaleType="fitEnd"
把原圖按比例放大(縮小)到適配 ImageView,並靠下顯示。
android:scaleType="fitStart"
把原圖按比例放大(縮小)到適配 ImageView,並靠上顯示。
android:scaleType="fitXY"
不保持原比例,直接把圖片拉伸填滿整個 ImageView。
由於 scaleType 只能決定「怎麼放」,無法決定「放多大」,所以更可靠的做法是:先取得屏幕寬度,再動態計算並設置控件高度。實現代碼如下:
package com.penngo.image;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b206_125);
int bwidth = bitmap.getWidth();
int bHeight = bitmap.getHeight();
int width = Screen.getScreenWidth(this);
Log.e("====", bwidth + " " + bHeight + " " + width);
ImageView img5 = (ImageView) this.findViewById(R.id.img5);
int height = width * bHeight / bwidth;
ViewGroup.LayoutParams para = img5.getLayoutParams();
para.height = height;
img5.setLayoutParams(para);
}
}
關鍵就三行:拿到原圖寬高、拿到屏幕寬度,再按原始比例反推高度寫回 LayoutParams。這樣圖片既鋪滿屏幕寬度,又不會變形。
<RelativeLayout 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:layout_margin="5dip"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img4"
android:src="@drawable/b206_125"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="@+id/view"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_below="@+id/img4"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#c3c3c3"
android:visibility="visible" />
<ImageView
android:id="@+id/img5"
android:src="@drawable/b206_125"
android:layout_below="@+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="@+id/view1"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_below="@+id/img5"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#c3c3c3"
android:visibility="visible" />
<ImageView
android:id="@+id/img6"
android:src="@drawable/b206_125"
android:scaleType="centerCrop"
android:layout_below="@+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

正確使用 Android 性能分析工具 TraceView
前面唠叨 最近公司 app 中有些列表在滑動時會有卡頓現象,我便著手解決這些問題。解決問題之前,首先要分析列表滑動的性能瓶頸在哪裡。因為之前不會正確使用 TraceVie
甲骨文訴谷歌終落幕:Android 生態與 Java 社區的雙重勝利
BI 中文站 5 月 27 日報道 美國陪審團的裁決剛剛揭曉:在甲骨文指控谷歌侵權一案中,甲骨文敗訴。陪審團認為,谷歌使用爭議程式碼屬於「合理使用」(fair use)。
Android Tab 類型介面實現方式總結
Tab 類型介面是 Android App 中非常常見的導覽方式。它讓使用者能在多個功能頁之間快速切換。本文整理五種常見實作方式,包含 TabActivity、ViewP
Android 圖片瘦身實戰:WebP 壓縮搭配 Gzip 傳輸的雙重優化
一、為什麼要盯著圖片體積不放 哪怕已經走進 4G 時代,流量依然是繞不開的敏感話題。而在行動網路的傳輸內容裡,最佔空間的載體毫無疑問是圖片——這也讓它成為行動端開發者必須