代码之家  ›  专栏  ›  技术社区  ›  wenus

如何实现位图函数

  •  0
  • wenus  · 技术社区  · 4 年前

    我在ImageView中有一个自定义扩展,用于半径特定的角:

        bitmap: Bitmap,
        topLeftCorner: Float, topRightCorner: Float,
        bottomRightCorner: Float, bottomLeftCorner: Float
    ): Bitmap? {
        val output = Bitmap.createBitmap(
            bitmap.width, bitmap.height,
            Bitmap.Config.ARGB_8888
        )
        val canvas = Canvas(output)
        val color = Color.WHITE
        val paint = Paint()
        val rect = Rect(0, 0, bitmap.width, bitmap.height)
        val rectF = RectF(rect)
        val path = Path()
        val radii = floatArrayOf(
            topLeftCorner, topLeftCorner,
            topRightCorner, topRightCorner,
            bottomRightCorner, bottomRightCorner,
            bottomLeftCorner, bottomLeftCorner
        )
        paint.isAntiAlias = true
        canvas.drawARGB(0, 0, 0, 0)
        paint.color = color
        path.addRoundRect(rectF, radii, Path.Direction.CW)
        canvas.drawPath(path, paint)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
        canvas.drawBitmap(bitmap, rect, rect, paint)
        return output
    }
    
    

    现在我正试图开始使用它,或者它真的有效,但我无法实现它。我无法将位图导入此函数。我该怎么做?问题是:

    val bitmap = <--- how get bitmap???
    
                binding.imgItem.createRoundedRectBitmap(
                    bitmap,
                    25.toFloat(),
                    25.toFloat(),
                    10.toFloat(),
                    8.toFloat()
                )```
    
    Any tips?
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Muhammad Bilal    4 年前

    步骤:1创建一个类(任何名称)

    class Utils {
    companion object {
    
        fun createRoundedRectBitmap(
            bitmap: Bitmap,
            topLeftCorner: Float, topRightCorner: Float,
            bottomRightCorner: Float, bottomLeftCorner: Float
        ): Bitmap? {
            val output = Bitmap.createBitmap(
                bitmap.width, bitmap.height,
                Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(output)
            val color = Color.WHITE
            val paint = Paint()
            val rect = Rect(0, 0, bitmap.width, bitmap.height)
            val rectF = RectF(rect)
            val path = Path()
            val radii = floatArrayOf(
                topLeftCorner, topLeftCorner,
                topRightCorner, topRightCorner,
                bottomRightCorner, bottomRightCorner,
                bottomLeftCorner, bottomLeftCorner
            )
            paint.isAntiAlias = true
            canvas.drawARGB(0, 0, 0, 0)
            paint.color = color
            path.addRoundRect(rectF, radii, Path.Direction.CW)
            canvas.drawPath(path, paint)
            paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
            canvas.drawBitmap(bitmap, rect, rect, paint)
            return output
        }
    
    }
    }
    

    步骤:2你可以这样得到位图

    val bitmap = AppCompatResources.getDrawable(this, R.drawable.imgItem)?.toBitmap()
    

    步骤:3

    binding.imgItem.setImageBitmap(bitmap?.let {
                ProgressUtils.loadBitmap(
                    it, 50.toFloat(),
                    50.toFloat(),
                    20.toFloat(),
                    8.toFloat()
                )
            })