allow drawing a bitmap as the canvas background

This commit is contained in:
tibbi 2017-04-09 12:23:23 +02:00
parent cc78d65f80
commit 631ef6cb97
2 changed files with 12 additions and 7 deletions

View File

@ -1,10 +1,7 @@
package com.simplemobiletools.draw package com.simplemobiletools.draw
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.graphics.*
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import android.util.AttributeSet import android.util.AttributeSet
@ -26,6 +23,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
private var mStartY = 0f private var mStartY = 0f
private var mIsSaving = false private var mIsSaving = false
private var mIsStrokeWidthBarEnabled = false private var mIsStrokeWidthBarEnabled = false
private var mDrawBitmapAsBackground: Bitmap? = null
init { init {
mPath = MyPath() mPath = MyPath()
@ -88,6 +86,11 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
return bitmap return bitmap
} }
fun drawBitmap(path: String) {
mDrawBitmapAsBackground = BitmapFactory.decodeFile(path)
invalidate()
}
fun addPath(path: MyPath, options: PaintOptions) { fun addPath(path: MyPath, options: PaintOptions) {
mPaths.put(path, options) mPaths.put(path, options)
pathsUpdated() pathsUpdated()
@ -96,6 +99,10 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
if (mDrawBitmapAsBackground != null) {
canvas.drawBitmap(mDrawBitmapAsBackground, 0f, 0f, null)
}
for ((key, value) in mPaths) { for ((key, value) in mPaths) {
changePaint(value) changePaint(value)
canvas.drawPath(key, mPaint) canvas.drawPath(key, mPaint)
@ -182,8 +189,6 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
} }
MotionEvent.ACTION_MOVE -> actionMove(x, y) MotionEvent.ACTION_MOVE -> actionMove(x, y)
MotionEvent.ACTION_UP -> actionUp() MotionEvent.ACTION_UP -> actionUp()
else -> {
}
} }
invalidate() invalidate()

View File

@ -117,7 +117,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
if (path.endsWith(".svg")) { if (path.endsWith(".svg")) {
Svg.loadSvg(this, File(path), my_canvas) Svg.loadSvg(this, File(path), my_canvas)
} else if (File(path).isImageSlow()) { } else if (File(path).isImageSlow()) {
my_canvas.drawBitmap(path)
} else { } else {
toast(R.string.invalid_file_format) toast(R.string.invalid_file_format)
} }