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

UI 組件之 ImageView 及其子類(一):ImageView 顯示圖片

編輯:關於 Android 程式設計

先來看看 ImageView 家族的繼承關系:

ImageView 繼承自 View 組件,主要功能是顯示圖片,也可以顯示 Drawable 對象。它的直接子類是 ImageButton、QuickContactBadge,間接子類是 ZoomButton。因此 ImageView 的 XML 屬性與方法,基本上都能用於 ImageButton 與 ZoomButton。

常用 XML 屬性

android:adjustViewBounds:是否調整自身邊界以保持圖片長寬比,需與 maxWidth、maxHeight 一起使用,單獨使用無效。

android:cropToPadding:為 true 時,組件會裁剪到保留 ImageView 的 padding。

android:maxHeight / android:maxWidth:設置最大高度與寬度,單獨使用無效,需配合 setAdjustViewBounds。若想固定大小又保持寬高比,需:1)設 adjustViewBounds 為 true;2)設 maxWidth、maxHeight;3)把 layout_width 與 layout_height 設為 wrap_content。

android:tint:將圖片渲染成指定顏色。

android:src:設置 ImageView 顯示的 Drawable 資源 ID。

android:scaleType:設置圖片的縮放或移動方式,以適應 ImageView 大小。

ScaleType 各取值

matrix:使用矩陣縮放。

fitXY:橫縱向獨立縮放,使圖片完全適應 ImageView,圖片橫縱比可能改變。

fitStart:保持橫縱比縮放,直到較長邊與 ImageView 邊長相等,置於左上角。

fitEnd:保持橫縱比縮放,置於右下角。

fitCenter:保持橫縱比縮放,置於中央。

center:圖片居中,不進行任何縮放。

centerInside:保持橫縱比縮放,使 ImageView 能完整顯示圖片。

centerCrop:保持橫縱比,使圖片完全覆蓋 ImageView,只要最短邊能顯示即可。

設置圖片資源的常用方法

setImageBitmap(Bitmap btm):用 Bitmap 位圖設置圖片。

setImageDrawable(Drawable drawable):用 Drawable 對象設置圖片(注意是實際資源對象,而非資源 ID)。

setImageResource(int resId):用圖片資源 ID 設置圖片。

實例:圖片浏覽器

下面通過一個小例子,演示切換圖片、調整透明度以及局部放大顯示。

布局文件 activity_main.xml:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button1" />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button2" />
        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/next" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:scaleType="fitCenter"
        android:src="@drawable/shuangta" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

MainActivity.java 的核心邏輯:

public class MainActivity extends Activity {
    int[] images = new int[]{
        R.drawable.lijiang, R.drawable.qiao, R.drawable.shuangta,
        R.drawable.shui, R.drawable.xiangbi,
    };
    int currentImg = 2;
    private int alpha = 255;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button plus = (Button) findViewById(R.id.button1);
        final Button minus = (Button) findViewById(R.id.button2);
        final Button next = (Button) findViewById(R.id.button3);
        final ImageView image1 = (ImageView) findViewById(R.id.imageView1);
        final ImageView image2 = (ImageView) findViewById(R.id.imageView2);

        // 查看下一張圖片
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image1.setImageResource(images[++currentImg % images.length]);
            }
        });

        // 改變透明度
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == plus) alpha += 20;
                if (v == minus) alpha -= 20;
                if (alpha >= 255) alpha = 255;
                if (alpha <= 0) alpha = 0;
                image1.setAlpha(alpha);
            }
        };
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);

        // 觸摸時顯示局部放大
        image1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                double scale = bitmap.getWidth() / 320.0;
                int x = (int) (event.getX() * scale);
                int y = (int) (event.getY() * scale);
                if (x + 120 > bitmap.getWidth()) x = bitmap.getWidth() - 120;
                if (y + 120 > bitmap.getHeight()) y = bitmap.getHeight() - 120;
                image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
                image2.setAlpha(alpha);
                return false;
            }
        });
    }
}

可以看到,借助 ImageView 豐富的 scaleType 與 setImageXxx 系列方法,再配合一點 Bitmap 裁剪,就能輕鬆做出圖片浏覽、透明度調節和局部放大等效果。

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