ythy / blog

Give everything a shot
6 stars 0 forks source link

Android Warning: Use `AppCompatResources.getDrawable()` #506

Open ythy opened 1 year ago

ythy commented 1 year ago

solution:

AS-IS

getDrawable(R.drawable.skin_theme_spring)

TO-BE

import androidx.appcompat.content.res.AppCompatResources
// 此用法会导致背景不透明 → AppCompatResources.getDrawable(baseContext,R.drawable.skin_theme_spring)
AppCompatResources.getDrawable(this,R.drawable.skin_theme_spring)
ythy commented 1 year ago

以上方案会导致 引用的背景不透明 正确:

import androidx.appcompat.content.res.AppCompatResources
AppCompatResources.getDrawable(this,R.drawable.skin_theme_spring)
ythy commented 1 year ago

画圆

val decoder = ImageDecoder.decodeDrawable(source) { decoder, _, _ ->
    val path = Path().apply {
        fillType = Path.FillType.INVERSE_EVEN_ODD
    }
    val paint = Paint().apply {
        isAntiAlias = true
        color = Color.TRANSPARENT
        xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC)
    }
    decoder.setPostProcessor { canvas ->
        val length = min(canvas.width.toFloat(),canvas.height.toFloat())
        val direction = Path.Direction.CW
        path.addRoundRect(0f, 0f, length, length, length / 2, length / 2, direction)
        canvas.drawPath(path, paint)
        PixelFormat.TRANSLUCENT
    }
}