Thursday, 27 May 2021

Drawing a bitmap to canvas and calling .setImageBitmap() causes image to be cropped

I'm simply drawing a bitmap from my drawable folder onto the canvas and setting that bitmap into an ImageView. It works perfectly fine on my Emulator, but on my real device why is it being cropped? It makes no sense. The code is exactly the same

Drawable backgroundDrawable = ContextCompat.getDrawable(context, R.drawable.landscape);

 try {
            Bitmap backgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
           
            int positionLeft = 0;
            int positionTop = 0;
            Bitmap mainBitmap = Bitmap.createBitmap(
                    backgroundBitmap.getWidth(),
                    backgroundBitmap.getHeight(),
                    Bitmap.Config.ARGB_8888
            );
            Canvas canvas = new Canvas(mainBitmap);
            canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop, null);   
            drawingPad.setImageBitmap(mainBitmap);
            
        } catch (Exception e) {
            Log.e(TAG, "onClickCreateImg: Error: ", e);
        }

On my emulator (Pixel 2) there is no issue:

enter image description here

But on my real device (Pixel 4a) the image is being automatically cropped? Why does this happen

enter image description here

I dont think it has anything to do with the layout, but here it is.

<?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"
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_create_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="Create image"/>

    <ImageView
        android:id="@+id/drawingPad"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/btn_create_image"
        app:layout_constraintBottom_toBottomOf="parent"
        android:paddingBottom="32dp"
        android:orientation="vertical"
        android:paddingTop="32dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Instead of using .setImageBitmap(mainBitmap) I used the Glide library instead

Glide.with(NewsFragment.this)
                   .load(mainBitmap)
                   .apply(RequestOptions.centerInsideTransform())
                   .into(drawingPad);

But this weird unexpected cropping issue is still happening only on my real device.

Tested on a 3rd device Emulator (Nexus 5X) doesn't get cropped either.

enter image description here

___________________________________________________

Tested on a 4th Real Device (Pixel 3XL) and it also gets cropped.

Still can't figure out why this is happening...



from Drawing a bitmap to canvas and calling .setImageBitmap() causes image to be cropped

No comments:

Post a Comment