Michael  Oelsner
Michael Oelsner

Reputation: 51

ImageView area is smaller than it's actual size

I have assigned a bitmap to a full-screen ImageView with canvas while streaming. Streaming works, the bitmap is shown in ImageView, but the active area of ImageView seems to be smaller than it's actual size. When I set the background color to ImageView, I get this result:

background fills only a small part of imageview

background fills only a small part of marked ImageView...

I assume this is the reason, why all my efforts to scale the bitmap to the size of ImageView do not work.

Here's my layout xml:

<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <ImageView
        android:id="@+id/camera_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#DCD5D5"
        android:rotation="90"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:onClick="take_photo"
        android:text="@string/take_photo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Assigning bitmap to ImageView:

public void run() {
    if (mLastFrame != null) {

        mutableBitmap = mLastFrame.copy(Bitmap.Config.RGB_565, true);
        if (moffset == OFFSET) {
            mBitmap = Bitmap.createBitmap(iv_heigth, iv_width, Bitmap.Config.RGB_565);
            mCameraView.setImageBitmap(mBitmap);
            mCanvas = new Canvas(mBitmap);
            mCanvas.drawBitmap(mutableBitmap, 0, 0, null);
            moffset += OFFSET;
        }

    }
}

ImageView size iv_width, iv_height is checked in onCreate function as follows:

ViewTreeObserver viewTreeObserver = mCameraView.getViewTreeObserver(); //check imageview size
if (viewTreeObserver.isAlive()) {
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            mCameraView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            //do some things
            iv_width = mCameraView.getWidth();
            iv_heigth = mCameraView.getHeight();
        }
    });
}

I am totally frustrated, hopefully, someone can help a lonely beginner...

Upvotes: 3

Views: 69

Answers (1)

Reaz Murshed
Reaz Murshed

Reputation: 24211

This is due to the rotation that was applied to the ImageView. If you remove the rotation, you will be able to see that the ImageView is back to normal. The height of the Imageview is actually matching parent and the same goes for width as well. However, when it is rotated by 90 degrees, you can see the width as the height of the ImageView and vice versa.

In order to tackle this problem, I would recommend removing the rotation from the xml layout that you applied to the ImageView. You can do that for your bitmap drawable instead. Before setting it into the ImageView, rotate the drawable by 90 degrees and then set it to the view.

Here are a few clever ways of rotating a bitmap by 90 degrees. I am just copying one answer from this post for convenience.

public static Bitmap rotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

I hope that helps!

Upvotes: 1

Related Questions