ImageView区域小于其实际大小

问题描述 投票:0回答:1

我在流式传输时已将位图分配给具有画布的全屏ImageView。流式传输有效,位图显示在ImageView中,但是ImageView的有效区域似乎小于其实际大小。将背景色设置为ImageView时,得到以下结果:

background fills only a small part of imageview

背景仅填充标记为ImageView ...的一小部分>

我假设这是原因,为什么我所有将位图缩放为ImageView大小的工作都无法正常进行。

这是我的布局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>

将位图分配给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大小iv_width,在iv_height功能中检查onCreate如下:

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();
        }
    });
}

我感到非常沮丧,希望有人可以帮助一个孤独的初学者...

我在流式传输时已为使用画布的全屏ImageView分配了位图。流式传输有效,位图显示在ImageView中,但是ImageView的活动区域似乎小于它的...

java android android-imageview android-bitmap image-rotation
1个回答
0
投票

这是由于应用于rotationImageView。如果删除旋转,您将可以看到ImageView恢复正常。 Imageview的高度实际上与父级匹配,宽度也一样。但是,将其旋转90度时,您可以看到宽度为ImageView的高度,反之亦然。

© www.soinside.com 2019 - 2024. All rights reserved.