我一直在尝试使用Google的此代码实验室:https://github.com/googlecodelabs/watchface-kotlin
对于不认识的人,Palette库允许您扫描位图并生成一组与您的应用样式匹配的Material Design颜色。
AbstractKotlinWatchFace.kt 文件中有一个用于初始化位图的方法:
private fun initializeBackground() {
backgroundImageEnabled =
analogWatchFaceStyle.watchFaceBackgroundImage.backgroundImageResource !=
EMPTY_IMAGE_RESOURCE
//We load our bitmap here
if (backgroundImageEnabled) {
backgroundBitmap = BitmapFactory.decodeResource(
resources,
analogWatchFaceStyle.watchFaceBackgroundImage.backgroundImageResource
)
}
在加载位图后,我现在可以使用调色板生成颜色:
Palette.from(backgroundBitmap).generate(Palette.PaletteAsyncListener {
if (it != null){
Log.d("Palette: ", "has been generated ")
//store color value
var firstColorValue = it.getLightVibrantColor(Color.WHITE)
}
})
在我无法解决的问题中,我们在 AnalogDslWatchFace kotlin文件中从DSL调用了该函数:
class AnalogDslWatchFace : AbstractKotlinWatchFace() {
override fun getWatchFaceStyle(): AnalogWatchFaceStyle {
/**
* Initializes colors and dimensions of watch face.
* Full code here: https://github.com/googlecodelabs/watchface-kotlin
*/
return analogWatchFaceStyle {
watchFaceColors {
main = Color.CYAN
highlight = Color.parseColor("#ffa500")
background = Color.WHITE
}
watchFaceDimensions {
hourHandRadiusRatio = 0.2f
minuteHandRadiusRatio = 0.5f
secondHandRadiusRatio = 0.9f
}
watchFaceBackgroundImage {
backgroundImageResource = R.drawable.globe_640
}
}
我想从此处访问调色板中生成的颜色,并从 AbstractKotlinWatchFace.kt 中传递调色板的值,以便在声明WatchFaceColors时可以这样做:main = palette.getLightVibrantColor(Color.WHITE)
highlight = palette.getLightVibrantColor(Color.WHITE)
shadow = palette.getDarkMutedColor(Color.BLACK)
等
任何代码示例都可以提供帮助