【问题标题】:Android: How to resize contents of an ImageView to the same dimensions of another ImageView's contents?Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?
【发布时间】:2021-06-16 09:19:39
【问题描述】:

我正在尝试将ImageView 的内容调整为与另一个ImageView 的内容相同的尺寸。例如,假设我有以下两个图像,其中一个是水平方向的,另一个是垂直方向的:

垂直图像:

水平图像:

我想将垂直图像调整为水平图像,如下所示:

最重要的是,ImageViews的尺寸和内容要一致,如下图ImageView的横屏截图:

我尝试为水平ImageView 检索BitMap 的尺寸,并将垂直ImageView 的BitMap 重新缩放为水平BitMap,但为BitMap 检索的尺寸始终显示原始水平照片的尺寸,而不是按比例缩放以适应水平ImageView 的照片尺寸。

以下是我迄今为止工作的代码:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/slider_screen_photo_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dip"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints">

        <ImageView
            android:id="@+id/slider_screen_image_1_box"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="370dp"
            android:scaleType="fitStart"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/slider_screen_image_2_box"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="370dp"
            android:scaleType="matrix"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java(检索 ImageView 的水平位图尺寸):

    ImageView sliderPreviewBox = null;

    if (photoNum == 1) {
        sliderPreviewBox = findViewById(R.id.slider_screen_image_1_box);
    }
    if (photoNum == 2) {
        sliderPreviewBox = findViewById(R.id.slider_screen_image_2_box);
    }

    Drawable drawable = sliderPreviewBox.getDrawable();
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();

【问题讨论】:

    标签: java android bitmap


    【解决方案1】:

    试试这个:

    onCreate..

    Bitmap bitmap = BitmapFactory.decodeFile(MyPath);
    try {
        int rotation = getExifOrientation(MyPath);
        Log.d(TAG, "\n--- Rotation of the image " + name + " is: " + rotation);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap rot_bitmap = rotateBitmap(MyPath, bitmap);
    imageView.setImageBitmap(rot_bitmap);
    
    

    之后:

    
    // To get the original orientation of the image
    public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
        try {
            // Getting the orientation of the image, there are 6 cases:
            // The Exif specification defines an Orientation Tag to indicate the orientation of the camera relative to the captured scene
            // Source: http://sylvana.net/jpegcrop/exif_orientation.html
            int rotation = getExifOrientation(src);
            if (rotation == 1) { return bitmap; }
            Matrix matrix = new Matrix();
            switch (rotation) {
                case 2: matrix.setScale(-1, 1); break;
                case 3: matrix.setRotate(180); break;
                case 4: matrix.setRotate(180); matrix.postScale(-1, 1); break;
                case 5: matrix.setRotate(90); matrix.postScale(-1, 1); break;
                case 6: matrix.setRotate(90); break;
                case 7: matrix.setRotate(-90); matrix.postScale(-1, 1); break;
                case 8: matrix.setRotate(-90); break;
                default: return bitmap;
            }
            // Creating a bitmap after its rotation and recycling the original one
            try {
                Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                // on low-memory system such as Android, must be attention to add bitmap.recycle(); to avoid the memory leak exception
                bitmap.recycle();
                return oriented;
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                return bitmap;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    
    
    // function to get the orientation of bitmap
    private static int getExifOrientation(String src) throws IOException {
        int orientation = 1;
        ExifInterface exif = new ExifInterface(src);
        String orientationString=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        try { assert orientationString != null;
            orientation = Integer.parseInt(orientationString); }
        catch(NumberFormatException e) { e.printStackTrace(); }
            return orientation;
        }
    
    

    【讨论】:

    • 我为什么要旋转图像?
    • 在您的情况下,它不会旋转照片。但是,如果您有其他方向的照片,它会的。为了适应 ImageView 你可以玩: android: scaleType = "centerInside"
    猜你喜欢
    • 2012-05-06
    • 2016-08-18
    • 1970-01-01
    • 2020-02-09
    • 2011-10-17
    • 2016-03-02
    • 2015-05-01
    • 2023-03-09
    • 2017-05-24
    相关资源
    最近更新 更多