Android Canvas clipRect, Kotlin

Android Canvas clipRect, Kotlin

 

 

    private fun mydraw() {
        val originBmp = BitmapFactory.decodeResource(resources, R.mipmap.pic).copy(Bitmap.Config.ARGB_8888, true)

        val newBmp = Bitmap.createBitmap(originBmp.width, originBmp.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(newBmp)

        //把原图绘制在画布Canvas
        canvas.drawBitmap(originBmp, 0f, 0f, null)

        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.color = Color.BLUE
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 30f

        val centerX = originBmp.width / 2
        val centerY = originBmp.height / 2
        val w = 300
        val h = 200
        val rect = Rect(centerX - w / 2, centerY - h / 2, centerX + w / 2, centerY + h / 2)
        canvas.clipRect(rect) //选(裁剪)出一块中心区域。
        iv1?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))

        canvas.drawColor(Color.RED) //在这块中心区域绘制颜色。
        iv2?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))

        canvas.drawRect(rect, paint) //在这块中心区域边框绘制线。
        iv3?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))
    }

    private fun saveBitmapToFile(bm: Bitmap): File? {
        var saveFile: File? = null
        val savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()
        if (!Files.exists(Paths.get(savePath))) {
            Log.d("保存文件", "${savePath}不存在!")
        } else {
            saveFile = File(savePath, System.currentTimeMillis().toString() + ".jpeg")
            try {
                val saveImgOut = FileOutputStream(saveFile)
                //压缩
                bm.compress(Bitmap.CompressFormat.JPEG, 90, saveImgOut)
                saveImgOut.flush()
                saveImgOut.close()
                Log.d("保存文件", "Bitmap保存至 ${saveFile.absoluteFile.toPath()}")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        return saveFile
    }

 

 

 

9ccabbf927fd4ff1b3e53cf087858d15.png

 

Android Canvas draws drawBitmap based on source Rect and destination Rect. The Kotlin-CSDN blog article has been viewed and read 1.3k times, liked 19 times, and collected 15 times. The article has been viewed and read 9.6k times. The article has been viewed and read 1.8k times. /*Java code converts Drawable to Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android Material Design: LinearLayoutCompat adds dividing line divider_linearlayout dividing line - CSDN Blog. https://blog.csdn.net/zhangphil/article/details/134818221

Android Bitmap is saved as a picture file on the mobile phone, and Kotlin_android bitmap saves the picture - CSDN blog article has been viewed and read 883 times, liked 8 times, and collected 5 times. Android splicing and merging pictures to generate a long picture code realizes merging two pictures, using the width of the first picture as the standard. If the width of the second picture to be merged is different from the first picture, then the width of the first picture shall prevail. line to scale the second image. Assume that there are already two pictures zhang.jpg and phil.jpg under Pictures in the root directory. Finally, these two pictures are merged into the long picture of zhangphil.jpg: package zhangphil.test;_android bitmap to save the picture https://blog.csdn .net/zhangphil/article/details/134603333

 

 

Guess you like

Origin blog.csdn.net/zhangphil/article/details/135156671