ImageView 的显示区域比实际尺寸要小。

3
我在流式传输时为全屏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;
        }

    }
}

onCreate函数中,以下代码检查ImageView的大小:iv_widthiv_height

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

我完全感到沮丧,希望有人能帮助一个孤独的初学者...

1个回答

1
这是由于应用于 ImageView 的旋转造成的。如果您移除旋转,您将能够看到 ImageView 回到正常状态。ImageView 的高度实际上是匹配父级的,宽度也是如此。但是,当它被旋转90度时,您可以看到宽度变成了 ImageView 的高度,反之亦然。
为了解决这个问题,我建议从应用于 ImageView 的 xml 布局中移除旋转。相反地,您可以对位图可绘制对象进行旋转。在将其设置到 ImageView 中之前,将可绘制对象旋转90度,然后将其设置到视图中。
这里有几种聪明的方法可以将位图旋转90度。为方便起见,我只复制了这篇文章中的一个答案。点击这里
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);
}

我希望这有所帮助!


1
很高兴知道我能帮到你!如果你觉得这个答案有用,请接受它! - Reaz Murshed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接