CanHub / Android-Image-Cropper

Image Cropping Library for Android, optimised for Camera / Gallery.
https://canhub.github.io/
Apache License 2.0
1.17k stars 240 forks source link

CropImageView fails to respect the match_parent size upon resizing the window #610

Open tasy5kg opened 5 months ago

tasy5kg commented 5 months ago

There are currently more and more users using large-screen Android devices, and these devices often allow users to resize the App window, which makes it necessary for us to display layouts correctly when the user resizes the window.

In my application, I have integrated the CropImageView from your project and set its width and height attributes to match_parent. However, upon resizing the window, the CropImageView fails to respect the match_parent setting, as illustrated in the attached screen recording:

https://github.com/CanHub/Android-Image-Cropper/assets/30869777/aea2b526-6aeb-4490-be95-13b9e1f40c88

To facilitate your understanding of the problem, I've prepared a simple, minimal reproducible project following these steps:

  1. Created an Empty Views Activity project.
  2. Added the dependency com.vanniktech:android-image-cropper:4.5.0.
  3. Set MainActivity to android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|fontScale|uiMode|orientation|density|screenSize|smallestScreenSize" in AndroidManifest.xml to prevent the Android system from recreating the entire activity upon changes in the application's window size.
  4. Placed a photo named image.webp in /res/raw/.
  5. Defined the contents of activity_main.xml and MainActivity.kt as provided in the attached code snippet:

    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
    tools:context=".MainActivity">
    
    <com.canhub.cropper.CropImageView
        android:id="@+id/crop_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:animateLayoutChanges="true"
        app:cropAutoZoomEnabled="false"
        app:cropShowProgressBar="false" />

```kotlin
package com.example.cropper

import android.graphics.BitmapFactory
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.canhub.cropper.CropImageView

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val cropImageView = findViewById<CropImageView>(R.id.crop_image_view)
    val bitmap = resources.openRawResource(R.raw.image).use {
      BitmapFactory.decodeStream(it)
    }
    cropImageView.setImageBitmap(bitmap)
  }
}

You may download this project file to quickly verify the issue: CropperViewSizeIssue.zip

While the screen recording was captured in the Windows Subsystem for Android, I have validated that this issue persists across Desktop virtual devices (in Android Studio) and MIUI physical devices.

Ideally, when the width and height of the CropImageView are set to match_parent, its size should consistently adhere to that of the parent layout, maintaining parity with the behavior of other Views, such as the ImageView below:

https://github.com/CanHub/Android-Image-Cropper/assets/30869777/f07b201f-732c-4873-96b6-2661780b7dac

I would greatly appreciate any insights or assistance you can provide in resolving this matter. Please let me know if further clarification or additional information is needed from my end. Thank you for your attention to this issue, and I look forward to your response.

vanniktech commented 5 months ago

Good find. Probably no one ever thought of this feature. Feel free to submit a PR which fixes this.