Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

讓圖片適應屏幕寬度:scaleType 全解與按比例縮放實作

編輯: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>

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved