First, the use of palette
Using palette allows us to pick a color from a picture, assign the picked color to the Actionbar,statusbar and UI background color to unify the interface tones or change the hue when loading different images
1. Use palette to add the following dependencies:
Implementation ' com.android.support:palette-v7:26.0.2 ' (Android Studio 3.0 Beta 5)
2. get Bitmap Object (or other bitmap object) in ImageView
Bitmap = ((iv as ImageView). Getdrawable () as bitmapdrawable). Bitmap
3, the incoming bitmap can get the palette object, the following is the synchronous and asynchronous use way:
synchronous fetch, which needs to be used in a child thread, otherwise causes the main thread to block
Val Palette:palette = Palette.from (bitmap). Generate ()
Asynchronous fetch, which can be used in the main thread
Palette.from (bitmap). Generate (Palette.paletteasynclistener {Palette:palette?
//......... Operation on the Palette object ....//
})
4, get the palette object, get the color, color corresponding to the following:
//1. Vitality Color
val vibrant:Palette.Swatch? = Palette?. Vibrantswatch
//2. Vibrant color of light
val lightbarant:Palette.Swatch? = Palette?. Lightmutedswatch
//3. Dark Vitality Color
val darkvibrant:Palette.Swatch? = Palette?. Darkvibrantswatch
//4. Soft color
val Munted:Palette.Swatch? = Palette?. Mutedswatch
//5. Bright Soft Color
val lightmuted:Palette.Swatch? = Palette?. Lightmutedswatch
//6. Dark Soft Color
val darkmuted:Palette.Swatch? = Palette?. Darkmutedswatch
5. Use the obtained color settings to the other UI
Modify Actionbar background color
Val Bar:actionbar = This!!. supportactionbar!!
Bar.setbackgrounddrawable (colordrawable (vibrant). rgb!!))
Modify the color of the button text
Bt.settextcolor (vibrant?). rgb!!)
Second, the example code
Override Fun OnCreate (Savedinstancestate:bundle?) {
super.oncreate (savedinstancestate)
Setcontentview (R.layout.activity_main)
bitmap = ((iv as ImageView). Getdrawable () as bitmapdrawable). Bitmap
Bt.setonclicklistener {
Palette.from (bitmap). Generate (Palette.paletteasynclistener {Palette:palette?
//1. Vitality Color
val vibrant:Palette.Swatch? = Palette?. Vibrantswatch
//2. Vibrant color of light
val lightbarant:Palette.Swatch? = Palette?. Lightmutedswatch
//3. Dark Vitality Color
val darkvibrant:Palette.Swatch? = Palette?. Darkvibrantswatch
//4. Soft color
val Munted:Palette.Swatch? = Palette?. Mutedswatch
//5. Bright Soft Color
val lightmuted:Palette.Swatch? = Palette?. Lightmutedswatch
//6. Dark Soft Color
val darkmuted:Palette.Swatch? = Palette?. Darkmutedswatch
//Modify Actionbar background color
val Bar:actionbar = this!!. supportactionbar!!
bar.setbackgrounddrawable (colordrawable (vibrant). rgb!!))
//Modify the color of the button text
bt.settextcolor (vibrant?). rgb!!)
})
}
}
three .
After clicking the button
The palette of Kotlin instance----android5.0 new features