两个工具方法
1
fun dp2px2(context: Context, dp: Int): Int {
val displayMetrics: DisplayMetrics = context.getResources().getDisplayMetrics()
return ((dp * displayMetrics.density + 0.5).toInt())
}
2 圆角的实现方法
fun Bitmap.toRoundedCorners(
topLeftRadius: Float = 0F,
topRightRadius: Float = 0F,
bottomRightRadius: Float = 0F,
bottomLeftRadius: Float = 0F
): Bitmap? {
val bitmap = Bitmap.createBitmap(
width, // width in pixels
height, // height in pixels
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
// the bounds of a round-rectangle to add to the path
val rectF = RectF(0f, 0f, width.toFloat(), height.toFloat())
// float array of 8 values, 4 pairs of [x,y] radii
val radii = floatArrayOf(
topLeftRadius, topLeftRadius, // top left