Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从图库中选择图像作为画布背景[Android]_Android_Kotlin_Canvas_Bitmap_Save - Fatal编程技术网

从图库中选择图像作为画布背景[Android]

从图库中选择图像作为画布背景[Android],android,kotlin,canvas,bitmap,save,Android,Kotlin,Canvas,Bitmap,Save,我已经成功地实现了保存到画廊后,我在画布上完成绘图,但现在我需要打开一个图像从厨房,例如我的旧画,并使用它作为背景,经过一些搜索没有答案,可以帮助我。我已经开始尝试这样做了,但是任何关于如何实现这一目标的建议或解决方案都将是非常棒的 这是我的画布自定义视图: 读写权限已在另一个类中授予和处理 var drawingColor: Int = ResourcesCompat.getColor(resources, R.color.colorBlack, null) var strok

我已经成功地实现了保存到画廊后,我在画布上完成绘图,但现在我需要打开一个图像从厨房,例如我的旧画,并使用它作为背景,经过一些搜索没有答案,可以帮助我。我已经开始尝试这样做了,但是任何关于如何实现这一目标的建议或解决方案都将是非常棒的

这是我的画布自定义视图:

读写权限已在另一个类中授予和处理

    var drawingColor: Int = ResourcesCompat.getColor(resources, R.color.colorBlack, null)
    var strokeDrawWidth: Float = 12f

    private var path = Path()

    private val paths = ArrayList<Triple<Path, Int, Float>>()
    private val undonePaths = ArrayList<Triple<Path, Int, Float>>()

    private val extraCanvas: Canvas? = null

    private var bitmapBackground: Bitmap? = null

    private var motionTouchEventX = 0f
    private var motionTouchEventY = 0f

    private var currentX = 0f
    private var currentY = 0f

    private val touchTolerance = ViewConfiguration.get(context).scaledTouchSlop

    private val paint = Paint().apply {
        color = drawingColor
        isAntiAlias = true
        isDither = true
        style = Paint.Style.STROKE
        strokeJoin = Paint.Join.ROUND
        strokeCap = Paint.Cap.ROUND
        strokeWidth = strokeDrawWidth
    }

    fun loadCanvasBackground(bitmap: Bitmap) {
        bitmapBackground = bitmap
        invalidate()
    }

    fun saveCanvasDrawing() {
        canvasCustomView.isDrawingCacheEnabled = true
        val extraBitmap: Bitmap = canvasCustomView.drawingCache
        MediaStore.Images.Media.insertImage(context.contentResolver, extraBitmap, "drawing", "Paint R")
    }

    fun resetCanvasDrawing() {
        path.reset()
        paths.clear()
        invalidate()
    }

    fun undoCanvasDrawing() {
        if (paths.size > 0) {
            undonePaths.add(paths.removeAt(paths.size - 1))
            invalidate()
        } else {
            Log.d("UNDO_ERROR", "Something went wrong with UNDO action")
        }
    }

    fun redoCanvasDrawing() {
        if (undonePaths.size > 0) {
            paths.add(undonePaths.removeAt(undonePaths.size - 1))
            invalidate()
        } else {
            Log.d("REDO_ERROR", "Something went wrong with REDO action")
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (bitmapBackground != null) {
            extraCanvas?.drawBitmap(bitmapBackground!!, 0f, 0f, paint)
        }

        for (p in paths) {
            paint.strokeWidth = p.third
            paint.color = p.second
            canvas?.drawPath(p.first, paint)
        }
        paint.color = drawingColor
        paint.strokeWidth = strokeDrawWidth
        canvas?.drawPath(path, paint)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event == null)
            return false

        motionTouchEventX = event.x
        motionTouchEventY = event.y

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                undonePaths.clear()
                path.reset()
                path.moveTo(motionTouchEventX, motionTouchEventY)
                currentX = motionTouchEventX
                currentY = motionTouchEventY
                invalidate()
            }

            MotionEvent.ACTION_MOVE -> {
                val distanceX = abs(motionTouchEventX - currentX)
                val distanceY = abs(motionTouchEventY - currentY)

                if (distanceX >= touchTolerance || distanceY >= touchTolerance) {
                    path.quadTo(
                        currentX,
                        currentY,
                        (motionTouchEventX + currentX) / 2,
                        (currentY + motionTouchEventY) / 2
                    )
                    currentX = motionTouchEventX
                    currentY = motionTouchEventY
                }
                invalidate()
            }

            MotionEvent.ACTION_UP -> {
                path.lineTo(currentX, currentY)
                extraCanvas?.drawPath(path, paint)
                paths.add(Triple(path, drawingColor, strokeDrawWidth))
                path = Path()
            }
        }
        return true
    }

    override fun isSaveEnabled(): Boolean {
        return true
    }

所以,没有人回答,经过一些搜索和实验,我得到了一个有效的解决方案,使用它或适应您的需要,如果您将得到相同的情况

答案基于我问题中的代码-因此,如果您遗漏了一些依赖项,请检查它

Init companion对象,带有活动中库操作的请求代码 创建一个方法,从需要接收Uri的库中拾取图像 然后需要重写onActivityResult方法来接收Uri并将其发送到自定义视图 现在,在自定义视图中的onDraw方法中,您需要使用.drawBitmap将收到的Uri(也称位图)设置为画布的背景
 companion object {
        private const val GALLERY_REQUEST_CODE = 102 
    }
    private fun pickFromGallery() {
            val intent = Intent(Intent.ACTION_PICK)
            intent.type = "image/*"
            val imageTypes = arrayOf("image/jpeg", "image/png")
            intent.putExtra(Intent.EXTRA_MIME_TYPES, imageTypes)
            startActivityForResult(intent, GALLERY_REQUEST_CODE)
        }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GALLERY_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                val uri: Uri? = data?.data
                if (uri != null) {
                    val bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, uri)
                    canvasCustomView.loadCanvasBackground(bitmap)
                }
            }
        }
    }
 override fun onDraw(canvas: Canvas?) {
        if (bitmapBackground != null) {
            canvas?.drawBitmap(bitmapBackground!!, 0f, 0f, paint)
        }