Android: Canvas is cleared on rotation

android-canvas clippath example
android surfacetexture
html canvas
html5 canvas rotate image around its center
canvas rotate center point
canvas rotate ctx
how to rotate canvas
canvas rotate around point

I implemented some functionality with my android application which is allow user to draw. But as we know on rotation activity and views is destroyed, so my Canvas is destroyed also, is there any way to save drawn state on rotation?

Any advice is appreciated.

UPD :

So the problem can be in next part of my code, I'm storing array of my path and color inside the view which one is DESTROYED on rotation also, and my arrays is cleared after that

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>>()

UPD 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>

UPD 3:

Any suggestions how it can be implemented with

onSaveInstanceState/onRestoreInstanceState

Use this line in manifest inside your Activity Tag

 android:configChanges="keyboardHidden|orientation"

Surface, Rotation constant: 0 degree rotation (natural orientation). int, ROTATION_180. Rotation constant: 180 Gets a Canvas for drawing into this surface. Canvas This API has been removed and is not supported. Do not use. I've got an Android application which maintains state regarding distance traveled, time elapsed, etc. This state I can conveniently store in an object and store a reference to that object in the Bundle when Android calls onDestroy() when the user changes the screen orientation, then restore the state in onCreate(Bundle savedBundle).

You can use Viewmodel for storing data related to UI elements. Storing data in viewmodel prevents it from being destroyed on configuration changes(or other changes which you haven't foreseen) and is generally considered a good practice.

Even then if you want to go forward with saveInstanceState or restoreInstanceState then you have to define a class which should contain data you want to store/restore and make it implement Parceable. Then on your activity you need to define onSaveInstanceState and 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);
}

Here model is the object of that class and key is a string used for storing that.

Drawing on Canvas :: Eloquent JavaScript, It is even possible to rotate or skew nodes with the transform style. The only way to move a shape on a canvas is to clear the canvas (or the part of the canvas � I am running on Android Lollipop 5.0.2. If I load up a previous signature, for example, into the pad's canvas as a BASE64 image then rotate the device to landscape mode, jSignature seems to remove the image altogether, leaving only the s

So partially success was made with

 android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"

Advanced Android 11.1 Part C: Apply clipping to Canvas objects, For the purpose of this codelab, clipping is a way to define regions of an image, canvas, or bitmap that are selectively drawn or not drawn onto the screen. When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.

html5-canvas, Move the canvas [0,0] origin to the shape's center point context.translate( shapeCenterX, shapeCenterY );. Rotate the canvas by the desired angle (in radians) The <canvas> element, introduced in HTML5, allows developers to dynamically create bit map graphics using JavaScript. In this tutorial you will learn about some of the basic operations supported

CanvasRenderingContext2D.rotate(), rotate() method of the Canvas 2D API adds a rotation to the transformation matrix. Syntax. void ctx.rotate(angle);. Parameters. angle: The rotation� Before we can start drawing, we need to talk about the canvas grid or coordinate space. Our HTML skeleton from the previous page had a canvas element 150 pixels wide and 150 pixels high. To the right, you see this canvas with the default grid overlayed. Normally 1 unit in the grid corresponds to 1 pixel on the canvas. The origin of this grid is positioned in the top left corner at coordinate

Using rotationAngle and touchRadius | Web, Deprecations and removals in Chrome 80 � WebVR 1.1 removed from Chrome Technically, the rotation angle is the number of degrees (between 0 and 90) of the contact (It should be noted that I only found out that Chrome on Android doesn't lock the Not to be outdone though here is a way to use it without canvas. /** Rotation constant: Unfreeze rotation (rotating the device changes its rotation state). public static final int ROTATION_UNFREEZE = - 2 ; /** Rotation constant: Freeze rotation to its current state.

Comments
  • can you please share your manifest code
  • Yeah, sure - added
  • You have to save/restore your custom view's data.