fix #95, handle bitmap redrawing after device rotation

This commit is contained in:
tibbi 2018-03-31 21:49:18 +02:00
parent de8c45fa21
commit 594718fd39
3 changed files with 21 additions and 4 deletions

View File

@ -37,6 +37,7 @@ import java.io.OutputStream
class MainActivity : SimpleActivity(), CanvasListener { class MainActivity : SimpleActivity(), CanvasListener {
private val FOLDER_NAME = "images" private val FOLDER_NAME = "images"
private val FILE_NAME = "simple-draw.png" private val FILE_NAME = "simple-draw.png"
private val BITMAP_PATH = "bitmap_path"
private var defaultPath = "" private var defaultPath = ""
private var defaultFilename = "" private var defaultFilename = ""
@ -47,6 +48,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
private var strokeWidth = 0f private var strokeWidth = 0f
private var isEraserOn = false private var isEraserOn = false
private var isImageCaptureIntent = false private var isImageCaptureIntent = false
private var lastBitmapPath = ""
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -208,6 +210,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
true true
} }
File(path).isImageSlow() -> { File(path).isImageSlow() -> {
lastBitmapPath = path
my_canvas.drawBitmap(this, path) my_canvas.drawBitmap(this, path)
defaultExtension = JPG defaultExtension = JPG
true true
@ -371,6 +374,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
my_canvas.clearCanvas() my_canvas.clearCanvas()
defaultExtension = PNG defaultExtension = PNG
defaultPath = "" defaultPath = ""
lastBitmapPath = ""
} }
private fun pickColor() { private fun pickColor() {
@ -404,6 +408,19 @@ class MainActivity : SimpleActivity(), CanvasListener {
redo.beVisibleIf(visible) redo.beVisibleIf(visible)
} }
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(BITMAP_PATH, lastBitmapPath)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
lastBitmapPath = savedInstanceState.getString(BITMAP_PATH)
if (lastBitmapPath.isNotEmpty()) {
openPath(lastBitmapPath)
}
}
private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener { private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
my_canvas.setStrokeWidth(progress.toFloat()) my_canvas.setStrokeWidth(progress.toFloat())

View File

@ -15,7 +15,7 @@ internal class MyParcelable : View.BaseSavedState {
for (i in 0 until size) { for (i in 0 until size) {
val key = parcel.readSerializable() as MyPath val key = parcel.readSerializable() as MyPath
val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat(), parcel.readInt() == 1) val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat(), parcel.readInt() == 1)
paths.put(key, paintOptions) paths[key] = paintOptions
} }
} }

View File

@ -27,9 +27,9 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
var mBackgroundBitmap: Bitmap? = null var mBackgroundBitmap: Bitmap? = null
var mListener: CanvasListener? = null var mListener: CanvasListener? = null
var mLastPaths = LinkedHashMap<MyPath, PaintOptions>() private var mLastPaths = LinkedHashMap<MyPath, PaintOptions>()
var mLastBackgroundBitmap: Bitmap? = null private var mLastBackgroundBitmap: Bitmap? = null
var mUndonePaths = LinkedHashMap<MyPath, PaintOptions>() private var mUndonePaths = LinkedHashMap<MyPath, PaintOptions>()
private var mPaint = Paint() private var mPaint = Paint()
private var mPath = MyPath() private var mPath = MyPath()