Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

ImageView 圖像控件詳解(上):屬性辨析與縮放模式

編輯:關於 Android 程式設計

(一)概述

ImageView 是最基礎的圖像顯示控件,除了展示圖片,還支持放大、縮小、旋轉等操作。本篇先把它的幾個核心屬性掰扯清楚。

(二)src 與 background 到底有什麼不同

先寫四個控件做對比實驗:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/maomi" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:src="@drawable/maomi" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/maomi" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@drawable/maomi" />
</LinearLayout>

運行效果:

規律很明顯:src 會按圖片本身的比例去適應容器,容器寬度被限制時圖片會等比縮小;而 background 則是無條件把圖片填滿整個容器,比例一概不管。

還有一點值得注意:如果同時指定了 width 和 height,background 依然老老實實填充,而 src 的尺寸則可能發生變化。

這就引出了 ImageView 的一個重要屬性——scaleType,也就是圖片縮放策略。這個內容比較多,放到後面細說。

(三)background 拉伸導致變形怎麼辦

上面的第四個效果圖裡,正方形已經被拽成了長方形,顯然很難看。目前常見的解法有兩種:

方法一:把尺寸寫死。動態添加 ImageView 時直接指定固定大小即可。

方法二:用 BitmapDrawable 的 XML 包裝一層,指定 gravity。

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/cat_bg"
    android:gravity="top"
    android:src="@drawable/maomi"
    android:tileMode="disabled" />

使用時,動態方式:ibtnCat.setBackgroundResource(R.drawable.cat);;靜態方式:android:background="@drawable/cat"。

補充:透明度設置。

src 與 background 結合的妙用

網上見過一張做法示意圖:

效果如下:

實現非常簡單——background 鋪一個純色,src 放圖片:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="#CCAADF"
    android:src="@drawable/maomi" />

兩者也可以在代碼中分別設置:

(四)adjustViewBounds:縮放時是否保持原圖長寬比

運行效果:

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.android_drawable_xml.MainActivity">

    <!-- 正常顯示 -->
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px"
        android:src="@drawable/maomi" />

    <!-- 限制最大寬高,並開啟邊界調整以保持長寬比 -->
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px"
        android:adjustViewBounds="true"
        android:maxHeight="200px"
        android:maxWidth="200px"
        android:src="@drawable/maomi" />
</LinearLayout>

加上 adjustViewBounds="true" 之後,即使限制了 max 尺寸,圖片也不會被拉變形。

(五)scaleType:決定圖片如何填充

一幅圖看懂它們幾個:

下面逐一做實驗。

1. fitStart / fitCenter / fitEnd

三者都是「等比縮放至完全顯示」,區別只在對齊位置。以 fitEnd 為例:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="300px"
        android:layout_height="300px"
        android:layout_margin="5px"
        android:background="#AAFD00"
        android:scaleType="fitEnd"
        android:src="@drawable/mengchong" />
</LinearLayout>

2. centerCrop 與 centerInside

兩者都居中,差別在於:centerCrop 撐滿容器並裁掉溢出部分;centerInside 則在圖片大於容器時縮小到能完整顯示。

<ImageView
    android:id="@+id/imageview2"
    android:layout_width="300px"
    android:layout_height="300px"
    android:layout_margin="5px"
    android:background="#AAFD00"
    android:scaleType="centerCrop"
    android:src="@drawable/mengchong" />

<ImageView
    android:id="@+id/imageview3"
    android:layout_width="300px"
    android:layout_height="300px"
    android:layout_margin="5px"
    android:background="#AAFD00"
    android:scaleType="centerInside"
    android:src="@drawable/mengchong" />

3. fitXY

不保持比例,直接把圖片拉伸到容器大小:

<ImageView
    android:id="@+id/imageview4"
    android:layout_width="300px"
    android:layout_height="300px"
    android:layout_margin="5px"
    android:background="#AAFD00"
    android:scaleType="fitXY"
    android:src="@drawable/mengchong" />

運行效果:

我選的圖比較小,所以看不出變化;換成一張尺寸較大的圖,就會明顯看到它被拉成「高瘦」或「矮胖」。

4. matrix

基於矩陣變換,不做任何自動縮放,是繪圖控件的常客:

<ImageView
    android:id="@+id/imageview"
    android:layout_width="300px"
    android:layout_height="300px"
    android:layout_margin="5px"
    android:background="#AAFD00"
    android:scaleType="matrix"
    android:src="@drawable/mengchong" />

運行效果:

5. center

保持原尺寸居中顯示,不放大也不縮小:

<ImageView
    android:id="@+id/imageview"
    android:layout_width="100px"
    android:layout_height="100px"
    android:layout_margin="5px"
    android:background="#AAFD00"
    android:scaleType="center"
    android:src="@drawable/meizi" />

運行效果:

以上就是 ImageView 較基礎的一部分內容,下篇進入實戰應用。

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