android - 通过捏而不是 Activity 旋转 View 时 getX() 和 getY() 的错误值

标签 android android-studio android-camera

这就是我想要做的。

我有一个相机 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/

相关文章:

android - 在 android 中使用 Texture atlas 和 openGL 1.1 的示例纹理

android - Android如何在android本地通知中添加视频和GIF?

android - 我们可以在 android 中为 TargetApi 注释样式吗

java - 构建依赖于 Android 项目 Gradle 的 Java 库

android - 如何获取从默认图像库中选择的图像的正确方向

iphone - 在 Unity3d 中打开 iPhone 和 Android 设备相机

android - 重新创建android Activity 而不眨眼

android - 配置kotlin插件更新失败?

android - com.android.camera.action.CROP 替代方案?

android-studio - 如何在flutter中禁用自动TODO注释?