我已经为全屏分配了一张位图。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();
}
});
}
我非常沮丧,希望有人能帮助一个孤独的初学者... ...
这是由于 rotation
适用于该地区的 ImageView
. 如果你去掉了旋转,你就能看到这个 ImageView
是恢复正常。的高度。Imageview
其实是与父体相匹配的,宽度也是如此。然而,当它旋转90度时,你可以看到宽度作为高度的 ImageView
反之亦然。
为了解决这个问题,我建议将旋转从 xml
的布局,您应用于 ImageView
. 你可以为你的位图绘制来代替。在将它设置为 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);
}
希望能帮到你