这就是我想要做的。
我有一个相机 View ,用户可以执行一些过程(缩放、移动和旋转)
当用户捕获图像时,我需要将它放在相机框的相同位置,并在相机 View 上应用进程
我这样做如下
mCameraView
是相机 View ,mCaptureImage
是预览图像
float x = mCameraView.getX();
float y = mCameraView.getY();
mCaptureImage.setX(x);
mCaptureImage.setY(y);
float angel = mCameraView.getRotation();
mCaptureImage.setRotation(angel);
float scaleX, scaleY;
scaleX = mCameraView.getScaleX();
scaleY = mCameraView.getScaleY();
mCaptureImage.setScaleX(scaleX);
mCaptureImage.setScaleY(scaleY);
如果用户不旋转框,一切正常
但是,如果用户旋转框,我得到错误的 x,y 值,我不知道为什么会发生这种情况,图像会跳转到未指定位置
我调试了一下,发现旋转 View 时x,y值发生了变化
这是XML的代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/selfie_move_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<com.otaliastudios.cameraview.CameraView
android:id="@+id/selfie_camera_view"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="gone"
app:cameraCropOutput="true"
app:cameraPlaySounds="false" />
<ImageView
android:id="@+id/selfie_capture_image"
android:layout_width="300dp"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:visibility="gone" />
</FrameLayout>
Exmaple about rotation view
谁能帮我解决这个问题?
最佳答案
屏幕的旋转将构成配置更改,这将更改“当前可用的屏幕尺寸”。为避免这种情况,您必须使用 configChanges
属性自行处理配置更改并指定一个值。
听起来您应该使用“screenSize”和“orientation”值。这将导致您的 Activity 调用 onConfigurationChanged()
而不是重新创建 Activity 。
Caution: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
有关处理配置更改的更多信息:https://developer.android.com/guide/topics/resources/runtime-changes
关于 configChange
属性的更多信息:https://developer.android.com/guide/topics/manifest/activity-element#config
关于android - 通过捏而不是 Activity 旋转 View 时 getX() 和 getY() 的错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50257299/