使用inBitmap选项重用位图

时间:2019-03-16 14:50:12

标签: android kotlin android-bitmap

我有一个customView,它经常被删除并重新创建,这会影响性能。我发现反复解码位图是我的应用程序滞后的原因。我尝试使用inBitMap重用位图,但是当我使用探查器检查性能时,它似乎没有任何改变。 This is how bitmaps look like

private fun drawDot(canvas: Canvas?,
                    dotColor: Int) {
    paint.color = dotColor
    paint.style = Paint.Style.FILL
    val bMapOptions = BitmapFactory.Options()

    val b:Bitmap = if(letter.isUpperCase()) {
        BitmapFactory.decodeResource(
            resources,
            resources.getIdentifier(letter.toLowerCase() + "_upper" +"_letter", "drawable", context.packageName),bMapOptions)!!
    } else {
        BitmapFactory.decodeResource(
            resources,
            resources.getIdentifier(letter + "_letter", "drawable", context.packageName),bMapOptions
        )
    }
    val scaled = Bitmap.createScaledBitmap(b, width/2, height/2, true)!!

    val box =BitmapFactory.decodeResource(
        resources,
        resources.getIdentifier("box", "drawable", context.packageName),bMapOptions)!!
      bMapOptions.inBitmap = b
      bMapOptions.inBitmap = box
    val boxScaled: Bitmap = Bitmap.createScaledBitmap(box, width, height, true)!!
    canvas?.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
    canvas?.drawBitmap(boxScaled, 0f, 0f, paint)

    canvas?.drawBitmap(scaled,width.toFloat()/2-width/4,height.toFloat()/2-height/4,paint)
    if (lineStyle == LetterPatternView.LINE_STYLE_INDICATOR &&
            (currentState == State.SELECTED)) {
        drawIndicator(canvas)
    }
}

0 个答案:

没有答案