Android:Canvas 在旋转时被清除
Android: Canvas is cleared on rotation
我用我的 android 应用程序实现了一些允许用户绘图的功能。但是正如我们所知,旋转 activity 并且视图被破坏,所以我的 Canvas 也被破坏,有没有办法在旋转时保存绘制状态?
如有任何建议,我们将不胜感激。
更新:
所以问题可能出在我的代码的下一部分,我将我的路径和颜色数组存储在视图中,其中一个在旋转时也被破坏,然后我的数组被清除
class CanvasCustomView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
companion object {
private const val STROKE_WIDTH = 12f
}
private var path = Path()
var drawColor : Int = ResourcesCompat.getColor(resources, R.color.colorBlack, null)
private val paths = ArrayList<Pair<Path, Int>>()
private val undonePaths = ArrayList<Pair<Path, Int>>()
更新 2:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.paintr">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
更新 3:
关于如何使用
实施的任何建议
onSaveInstanceState/onRestoreInstanceState
在您的 Activity 标签内的清单中使用这一行
android:configChanges="keyboardHidden|orientation"
您可以使用 Viewmodel 来存储与 UI 元素相关的数据。在视图模型中存储数据可以防止它在配置更改(或您未预见的其他更改)时被破坏,并且通常被认为是一种好的做法。
即便如此,如果您想继续使用 saveInstanceState 或 restoreInstanceState,那么您必须定义一个 class,它应该包含您想要 store/restore 的数据并使其实现 Parceable。然后在你的 activity 上你需要定义 onSaveInstanceState 和 onRestoreInstanceState。
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState);
outState?.putParcelable(key, model);
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState);
model = outState?.getParcelable(key);
}
这里的model是那个class的对象,key是用来存储那个的字符串
因此
取得了部分成功
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
我用我的 android 应用程序实现了一些允许用户绘图的功能。但是正如我们所知,旋转 activity 并且视图被破坏,所以我的 Canvas 也被破坏,有没有办法在旋转时保存绘制状态?
如有任何建议,我们将不胜感激。
更新:
所以问题可能出在我的代码的下一部分,我将我的路径和颜色数组存储在视图中,其中一个在旋转时也被破坏,然后我的数组被清除
class CanvasCustomView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
companion object {
private const val STROKE_WIDTH = 12f
}
private var path = Path()
var drawColor : Int = ResourcesCompat.getColor(resources, R.color.colorBlack, null)
private val paths = ArrayList<Pair<Path, Int>>()
private val undonePaths = ArrayList<Pair<Path, Int>>()
更新 2:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.paintr">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
更新 3:
关于如何使用
实施的任何建议onSaveInstanceState/onRestoreInstanceState
在您的 Activity 标签内的清单中使用这一行
android:configChanges="keyboardHidden|orientation"
您可以使用 Viewmodel 来存储与 UI 元素相关的数据。在视图模型中存储数据可以防止它在配置更改(或您未预见的其他更改)时被破坏,并且通常被认为是一种好的做法。
即便如此,如果您想继续使用 saveInstanceState 或 restoreInstanceState,那么您必须定义一个 class,它应该包含您想要 store/restore 的数据并使其实现 Parceable。然后在你的 activity 上你需要定义 onSaveInstanceState 和 onRestoreInstanceState。
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState);
outState?.putParcelable(key, model);
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState);
model = outState?.getParcelable(key);
}
这里的model是那个class的对象,key是用来存储那个的字符串
因此
取得了部分成功 android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"