Java ImageView区域比它小';s实际尺寸
我已将位图分配给流式处理时带有画布的全屏Java ImageView区域比它小';s实际尺寸,java,android,android-imageview,android-bitmap,image-rotation,Java,Android,Android Imageview,Android Bitmap,Image Rotation,我已将位图分配给流式处理时带有画布的全屏ImageView。流式处理工作正常,位图显示在ImageView中,但ImageView的活动区域似乎小于其实际大小。当我将背景色设置为ImageView时,我得到以下结果: 背景仅填充标记的图像视图的一小部分 我想这就是我将位图缩放为ImageView大小的所有努力都不起作用的原因 以下是我的布局xml: <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http
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
sizeiv_-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
的高度实际上与父视图匹配,宽度也是如此。但是,当它旋转90度时,您可以将宽度视为图像视图的高度,反之亦然
为了解决这个问题,我建议从应用于ImageView
的xml
布局中删除旋转。你可以这样做,你的位图绘制代替。在将其设置到图像视图
中之前,将可绘制图像旋转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);
}
我希望这有帮助 很高兴知道我能帮忙!如果您觉得这有帮助,请接受答案!
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);
}