@@ -14,11 +14,12 @@ import com.simplemobiletools.commons.extensions.getContrastColor
1414import java.util.*
1515
1616class MyCanvas (context : Context , attrs : AttributeSet ) : View(context, attrs) {
17- var mPaths: MutableMap <MyPath , PaintOptions >
17+ var mPaths = LinkedHashMap <MyPath , PaintOptions >()
1818 var mBackgroundBitmap: Bitmap ? = null
19- private var mPaint: Paint
20- private var mPath: MyPath
21- private var mPaintOptions: PaintOptions
19+ private var mPaint = Paint ()
20+ private var mPath = MyPath ()
21+ private var mPaintOptions = PaintOptions ()
22+ private var mIsNothingDrawn = true
2223
2324 private var mListener: PathsChangedListener ? = null
2425 private var mCurX = 0f
@@ -29,9 +30,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
2930 private var mIsStrokeWidthBarEnabled = false
3031
3132 init {
32- mPath = MyPath ()
33- mPaintOptions = PaintOptions ()
34- mPaint = Paint ().apply {
33+ mPaint.apply {
3534 color = mPaintOptions.color
3635 style = Paint .Style .STROKE
3736 strokeJoin = Paint .Join .ROUND
@@ -40,8 +39,6 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
4039 isAntiAlias = true
4140 }
4241
43- mPaths = LinkedHashMap <MyPath , PaintOptions >()
44- mPaths.put(mPath, mPaintOptions)
4542 pathsUpdated()
4643 }
4744
@@ -53,7 +50,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
5350 if (mPaths.isEmpty())
5451 return
5552
56- val lastKey: MyPath ? = mPaths.keys.lastOrNull()
53+ val lastKey = mPaths.keys.lastOrNull()
5754
5855 mPaths.remove(lastKey)
5956 pathsUpdated()
@@ -220,7 +217,6 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
220217 public override fun onSaveInstanceState (): Parcelable {
221218 val superState = super .onSaveInstanceState()
222219 val savedState = SavedState (superState)
223-
224220 savedState.paths = mPaths
225221 return savedState
226222 }
@@ -230,19 +226,18 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
230226 super .onRestoreInstanceState(state)
231227 return
232228 }
233- val savedState = state
234- super .onRestoreInstanceState(savedState.superState)
235229
236- mPaths = savedState.paths
230+ super .onRestoreInstanceState(state.superState)
231+ mPaths = state.paths
237232 pathsUpdated()
238233 }
239234
240235 internal class SavedState : View .BaseSavedState {
241- var paths: MutableMap <MyPath , PaintOptions > = HashMap ()
236+ var paths = LinkedHashMap <MyPath , PaintOptions >()
242237
243238 companion object {
244239 val CREATOR : Parcelable .Creator <SavedState > = object : Parcelable .Creator <SavedState > {
245- override fun newArray (size : Int ): Array < SavedState > = arrayOf()
240+ override fun newArray (size : Int ) = arrayOf< SavedState > ()
246241
247242 override fun createFromParcel (source : Parcel ) = SavedState (source)
248243 }
@@ -253,8 +248,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
253248 override fun writeToParcel (out : Parcel , flags : Int ) {
254249 super .writeToParcel(out , flags)
255250 out .writeInt(paths.size)
256- for ((key , paintOptions) in paths) {
257- out .writeSerializable(key )
251+ for ((path , paintOptions) in paths) {
252+ out .writeSerializable(path )
258253 out .writeInt(paintOptions.color)
259254 out .writeFloat(paintOptions.strokeWidth)
260255 }
0 commit comments