【问题标题】:ImageView area is smaller than it's actual sizeImageView 区域小于它的实际大小
【发布时间】:2020-05-11 16:39:35
【问题描述】:

在流式传输时,我已将位图分配给带有画布的全屏 ImageView。流媒体工作,位图显示在ImageView,但ImageView 的活动区域似乎小于它的实际大小。当我将背景颜色设置为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();
        }
    });
}

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

【问题讨论】:

    标签: java android android-imageview android-bitmap image-rotation


    【解决方案1】:

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

    为了解决这个问题,我建议从您应用于ImageViewxml 布局中删除旋转。您可以改为为您的位图可绘制对象执行此操作。在将其设置为ImageView 之前,将drawable 旋转90 度,然后将其设置为视图。

    Here are a few clever ways 将位图旋转 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);
    }
    

    希望对你有帮助!

    【讨论】:

    • 很高兴知道我能帮上忙!如果您觉得这有帮助,请接受答案!
    猜你喜欢
    • 1970-01-01
    • 2018-04-04
    • 2015-10-16
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多