The palette of Kotlin instance----android5.0 new features

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.