效果图
关键代码
kotlin
// 将图片转换为bitmap对象
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.pg)
// 根据bitmap对象生成palette
Palette.from(bitmap).generate { palette ->
// 获取palette的六种颜色
//有活力的
val vibrantColor: Int = palette.getVibrantColor(Color.BLUE)
// 有活力的,暗色
val darkVibrantColor: Int = palette.getDarkVibrantColor(Color.BLUE)
// 有活力的,亮色
val lightVibrantColor: Int = palette.getLightVibrantColor(Color.BLUE)
// 柔和的
val mutedColor: Int = palette.getMutedColor(Color.BLUE)
// 柔和的,暗色
val darkMutedColor: Int = palette!!.getDarkMutedColor(Color.BLUE)
// 柔和的,亮色
val lightMutedColor: Int = palette.getLightMutedColor(Color.BLUE)
tvVibrantColor.setBackgroundColor(vibrantColor)
tvDarkVibrantColor.setBackgroundColor(darkVibrantColor)
tvLightVibrantColor.setBackgroundColor(lightVibrantColor)
tvMutedColor.setBackgroundColor(mutedColor)
tvDarkMutedColor.setBackgroundColor(darkMutedColor)
tvLightMutedColor.setBackgroundColor(lightMutedColor)
}
Palette
是一个可以从图片(Bitmap
)中提取颜色的工具,可以使UI
根据不同的图片显示不同的颜色。
Palette
可以提取如下六种颜色:
Vibrant
(有活力的)Vibrant dark
(有活力的 暗色)Vibrant light
(有活力的 亮色)Muted
(柔和的)Muted dark
(柔和的 暗色)Muted light
(柔和的 亮色)