gpt4 book ai didi

java - ImageView 区域小于它的实际大小

转载 作者:行者123 更新时间:2023-12-01 14:32:18 24 4
gpt4 key购买 nike

我在流式传输时将位图分配给带有 Canvas 的全屏 ImageView。流式传输有效,位图显示在 ImageView 中,但 ImageView 的 Activity 区域似乎小于实际大小。当我将背景颜色设置为 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 size iv_width, iv_heightonCreate 函数中检查如下:

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

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

最佳答案

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

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

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

希望对您有所帮助!

关于java - ImageView 区域小于它的实际大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61734752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com