方向改变时手势会清除
我的布局中有一个gestureOverlayView,用于绘制数字签名。当我以某个方向(例如横向)绘制然后更改方向时,问题就出现了——overlayView 就被清除了。我尝试过包括 onConfigurationChanged(); 但没有效果。我还尝试使用 onSaveInstance 和 onRestoreInstance 进行以下操作,但它没有给我解决方案:
@Override
protected void onSaveInstanceState(Bundle outState) {
Gesture gesture = overlay.getGesture();
outState.putParcelable("gesture", (Parcelable) gesture);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Gesture gesture = (Gesture) savedInstanceState.getParcelable("gesture");
overlay.setGesture(gesture);
mDoneButton.setEnabled(true);
super.onRestoreInstanceState(savedInstanceState);
}
我也尝试过包括:
android:configChanges="fontScale|uiMode|screenLayout|navigation|touchscreen|mcc|mnc|orientation|keyboardHidden|keyboard">
但即使这样也没有用。
有人有解决这个问题的办法吗?
I have a gestureOverlayView in my layout which I am using for drawing digital Signature. The problem arises when I draw in a certain orientation (say landscape) and then change the orientation - the overlayView just clears. I have tried including onConfigurationChanged();but no effect. I have also attempted the following with onSaveInstance and onRestoreInstance, but it gives me no solution:
@Override
protected void onSaveInstanceState(Bundle outState) {
Gesture gesture = overlay.getGesture();
outState.putParcelable("gesture", (Parcelable) gesture);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Gesture gesture = (Gesture) savedInstanceState.getParcelable("gesture");
overlay.setGesture(gesture);
mDoneButton.setEnabled(true);
super.onRestoreInstanceState(savedInstanceState);
}
I have also tried including:
android:configChanges="fontScale|uiMode|screenLayout|navigation|touchscreen|mcc|mnc|orientation|keyboardHidden|keyboard">
But even this is of no use.
Does anyone have a solution for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在我的应用程序中尝试了您的代码并遇到了同样的问题。 getGesture() 的结果在 onSaveInstanceState、onStop 和 onPause 中为 null,因此我猜测 GestureOverlayView 在屏幕旋转和调用这些方法之间的某个位置无效。
解决方法是重写 onGesturePerformed,假设您的 Activity 实现 OnGesturePerformedListener 并且您在叠加层上调用 addOnGesturePerformedListener(this),并保存最近手势的成员实例。类似于:
然后在 onSaveInstanceState 中,只需获取并打包成员实例,而不是调用 getGesture()。现在,当 Activity 重新创建自身时,setGesture() 应该按预期工作。
I tried your code in my application and had the same issue. The result of getGesture() is null within onSaveInstanceState, onStop, and onPause, so I'm guessing that the GestureOverlayView is invalidated somewhere between the screen rotation and the calling of those methods.
A workaround is to override onGesturePerformed, assuming your Activity implements OnGesturePerformedListener and you call addOnGesturePerformedListener(this) on your overlay, and save a member instance of the most recent Gesture. Something like:
Then in your onSaveInstanceState, simply get and package the member instance instead of calling getGesture(). Now setGesture() should work as expected when the Activity recreates itself.
在清单中使用 android:configChanges="fontScale|uiMode|screenLayout|navigation|touchscreen|mcc 或尝试 onSavedInstance;
Use android:configChanges="fontScale|uiMode|screenLayout|navigation|touchscreen|mcc in manifest or try onSavedInstance;
当方向改变时,它会重新启动活动。您可以尝试在清单文件中限制应用程序的方向。
When the orientation changes it restarts the activity. You could try restrict the orientation of the app in the manifest file.