These days to learn some of the new features of the android5.0 version of the SDK, tossing for a long time. Androidstudio was repeatedly installed and uninstalled and installed, in Eclipse and Androidstudio
Toss back and forth between. I didn't expect the SDK to be upgraded to version 5.0, so much trouble. At first I wanted to continue with eclipse, but I was tortured by various upgrade plugins and guides,
Instead of Androidstudio, the computer always gets stuck in a dog! I have no words to die, and later in a variety of toss, eventually abandoned eclipse, using Androidstuidio. Now it's still
Not very accustomed to the operation of Androidstudio. Will gradually get used to it.
All right, spit out the groove. Now that the tools have been set, you can't wait to see what new features have been added to Android 5.0X. Today I'm going to talk about the first new feature of my study,
that is, the palette palette. Because this palette can extract the tones you need from a bitmap image, it's great for developers to keep the app's color view harmonious and unified.
Here is my harvest and a small example.
First, the preparation before use
First, you need to add dependencies to the Gradle.
That is, add appconat-v7 and palette-v7 dependencies to your build.gradle dependencies. As follows:
dependencies {
Compile ' com.android.support:appcompat-v7:21.0.3 '
Compile ' com.android.support:palette-v7:21.+ '
}
Second, about the use of Palette API
The first is to get to a palette object, there are four ways to get it, as follows:
A
Palette p = palette.generate (bitmap);
Two
Palette p = palette.generate (bitmap, 24);
Three
Palette.generateAsync(bitmap,
new
Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
}
});
Four
Palette.generateAsync(bitmap, 24,
new
Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
}
});
Note: (1) The first two methods are obtained synchronously, and the latter two are obtained asynchronously, whichever is used. Asynchronously acquires the palette in the
The parameters of the Ongenerated method. So the logic of general color setting is also in this method.
(2) When you get the palette object, you can specify its size. Generally, the larger the size, the longer it takes to get it. Do not specify
, the default palette size, which is size 16.
(3) How much size is appropriate? In general, such as avatar settings, preferably in the 24~32, large landscapes, such as, generally in
Between the 8~16.
Get to palette it's time to use it, and then get to a swatch with a sample from the palette object, there are 6 samples , as follows:
Palette.swatch s = P.getvibrantswatch (); Get to the vibrant hue of this
Palette.swatch s = P.getdarkvibrantswatch (); Get a vibrant black
Palette.swatch s = P.getlightvibrantswatch (); Get a vibrant bright
Palette.swatch s = P.getmutedswatch (); Get a soft tint
Palette.swatch s = P.getdarkmutedswatch (); Get the soft Black
Palette.swatch s = P.getlightmutedswatch (); Get a soft light
Finally, we can use the sampled Tones sample Swatch object to give color to what is needed, there are several ways:
getPopulation(): the amount of pixels which
this
swatch represents.
getRgb(): the RGB value of
this
color.
getHsl(): the HSL value of
this
color.
getBodyTextColor(): the RGB value of a text color which can be displayed on top of
this
color.
getTitleTextColor(): the RGB value of a text color which can be displayed on top of
this
color.
For example, if your textview has a background image, you can use palette to get the hue of the background image, and then use the Getbodytextcolor
To set the color of this textview text to match the background image!
Three, a small example
Create a new project with Androidstudio, and then modify its Activity_main.xml code. As follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "vertical">7 8 <TextView9 Android:id= "@+id/mytext_view"Ten Android:layout_width= "Match_parent" One Android:layout_height= "Wrap_content" A android:textsize= "30SP" - Android:text= "Hello world!" /> - <Button the Android:id= "@+id/btn" - Android:layout_width= "Match_parent" - Android:layout_height= "50DP" - Android:background= "@drawable/palette" + Android:text= "I am the button"/> - </LinearLayout>
It's easy to put a textview and a button and set a background image for the button. The next thing I'm going to do is from
This background image extracts the hue to give the entire layout color. The code to modify Mainactivity is as follows:
1 Packagekun.fuly.myapplication;2 3 ImportAndroid.annotation.TargetApi;4 ImportAndroid.graphics.Bitmap;5 Importandroid.graphics.BitmapFactory;6 Importandroid.graphics.drawable.ColorDrawable;7 ImportAndroid.os.Build;8 ImportAndroid.os.Bundle;9 Importandroid.support.v7.app.ActionBarActivity;Ten ImportAndroid.support.v7.graphics.Palette; One ImportAndroid.widget.Button; A ImportAndroid.widget.TextView; - - the - Public classMainactivityextendsactionbaractivity { - - PrivateBitmap bmp; + PrivateTextView MyText; - PrivateButton btn; + A at @TargetApi (build.version_codes. Jelly_bean) - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - -BMP =Bitmapfactory.decoderesource (Getresources (), r.drawable.palette); in -MyText =(TextView) Findviewbyid (R.id.mytext_view); to +BTN =(Button) Findviewbyid (R.ID.BTN); - thePalette p =palette.generate (BMP); * $Palette.swatch s =P.getvibrantswatch ();Panax Notoginseng - //set the background color and text color for TextView theMytext.setbackground (Newcolordrawable (S.getrgb ())); + A Mytext.settextcolor (S.getbodytextcolor ()); the + //set the color of the button's text - Btn.settextcolor (S.getbodytextcolor ()); $ $ - //set color for Actionbar -Getsupportactionbar (). Setbackgrounddrawable (Newcolordrawable (S.getrgb ())); the } - Wuyi the}
The code is simple and there's nothing to explain. Operation, the effect is as follows:
Well, it still looks ugly, but the color is harmonious. believe that palette in your hand can be used very beautiful. The rest will not be said,
This concludes the introduction of palette.
Android 5.X use palette