android - Android:画布在旋转时被清除
问题描述
我用我的 android 应用程序实现了一些允许用户绘图的功能。但是我们知道旋转活动和视图被破坏了,所以我的画布也被破坏了,有什么方法可以保存旋转时的绘制状态?
任何建议表示赞赏。
更新:
所以问题可能出在我的代码的下一部分,我将路径和颜色的数组存储在视图中,其中一个也在旋转时被销毁,之后我的数组被清除
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
解决方案
在活动标签内的清单中使用此行
android:configChanges="keyboardHidden|orientation"
推荐阅读
- node.js - 在不等待响应的情况下写入 MongoDB
- ios - 如何在使用 Core Data 的同时使用部分的标题显示一个部分?
- java - 从字符串中提取一些字符串的更好方法
- youtube - YouTube API v3 搜索列表 - 返回较少结果的关键字短语
- sql - 多次加入后在自加入中找到最大值
- parsing - ANTLR4:如何禁用/启用跳过规则中的空格?
- android - 初始化活动时,连接到 Firebase 的我的应用程序未设置 recyclerView
- python - TypeError:无法处理此数据类型 - `PIL.Image.fromarray` 的模式错误?
- html - 使用 HTML/CSS 居中导航栏
- javascript - Gutenberg 块验证失败(缺少属性)