首页 > 解决方案 > 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

标签: androidkotlincanvasrotation

解决方案


在活动标签内的清单中使用此行

 android:configChanges="keyboardHidden|orientation"

推荐阅读