The palette of Android5.0 is simple and practical

Source: Internet
Author: User

Always wanted to learn the new features of android5.0, but because of the simulator's reasons, has not yet this wish.

Today is a tragic day, but there is no such an old saying, "disguise, do not know the blessing." First talk about today, do not know what happened, genymotion data all lost, no way, can only re-download new data, is in the process of selecting a version, suddenly found 5.0, good excitement, download down to experience a.


UI is not particularly cool, feel can kill iOS several streets, good, not nonsense, into today's theme palette.

Palette, Chinese name: color palette, that is, Android allows us to dynamically get colors, as to how these colors are handled, depends on your personal hobbies and needs.

So how do we get the color? Where do I get the color?

First to answer the second question, in general, we are grabbing the color from the picture and then setting it to the description of the picture (text color and background color). What is the benefit of doing this? The picture and text are guaranteed to be in the same hue. Isn't that cool?

Let's take a look at the first question how to get it? The answer is also very simple, the palette we are going to use today.

According to the Convention, first on the code, OH, no, before the code, you have to say how palette use, well, find your ANDROID5 SDK down directory, and then navigate to the Sdk\extras\android\support\v7\palette directory, Then open the Libs directory, you can see there is a Android-support-v7-palette.jar file, copy this file to your project Libs directory, so that your project can use palette.

The preparation is complete, so let's take a look at the implementation code.

First look at the layout file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "org.loader.palette.MainActivity" >    <imageview        android:id= "@+id/iv"        Android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:src= "@drawable/ic_ Launcher "/>    <textview        android:layout_below=" @id/iv "        android:id=" @+id/tv "        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "@string/hello_world"/ ></RelativeLayout>

Very simple, a imageview and a textview, after the meeting, we will be through the code, according to the ImageView image on the main tone to adjust the textview tones.

public class Mainactivity extends Activity {private ImageView mimageview;private TextView Mtextview;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);                Mimageview = (ImageView) Findviewbyid (R.ID.IV);        Mtextview = (TextView) Findviewbyid (r.id.tv);        Bitmap bmp = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);                Palette.generateasync (BMP, New Palette.paletteasynclistener () {@Overridepublic void ongenerated (Palette Palette) { Palette.swatch swatch = Palette.getvibrantswatch (); if (null! = Swatch) {Mtextview.setbackgroundcolor (Swatch.getrgb ()) ; Mtextview.settextcolor (Swatch.gettitletextcolor ());}});}}    

Before introducing the code, let's start with the palette class, look at the documentation, this class does not provide the available construction methods, and provides a series of static generate and Generateasync methods, very well understood, is the meaning of the generation, To say that the difference between generate and generateasync, no matter what code, to manipulate a picture, it is certainly a waste of time, because the code to scan every pixel, so we can not go to the UI thread to do so, so the Generate method is generally used in the online, And wait for the result to be returned; we can use the Generateasync method in the UI thread, and any one of the overloads of the method has a callback parameter, and its return value is a asynctask.

OK, let's start looking at our code.

12 lines, we get a bitmap, this is nothing to say, this bitmap is ImageView on the picture shown.

14~23 line is the key, you can see that we used the palette static method Generateasync. The first parameter is a bitmap object, exactly, we pass 12 rows to get the bitmap to it, the second parameter is a callback interface, in this interface there is a ongenerated method, the parameter is a palette object. Such a tortuous process, we finally acquired the palette object.

If, you read the document, you will find that palette provides a series of Get methods, here we are using Getdarkvibrantswatch, which returns a Swatch (Swatch) object, and then we can get the desired color through swatch. Swatch,getrgb () returns a RBG color on the Swatch, Swatch.gettitletextcolor () returns an Android-recommended color for any title, plus a Getbodytextcolor () Back is the Android recommended one for any body color, ha, isn't it smart?

The code is simple, but the effect of the implementation is exciting, and finally look at the effect:


is not based on the color of the picture, the dynamic change of textview tones.

More practical features, we also need to dig in the daily development, we can do a gourmet app, text introduction of the color and the color of the picture of the food related, so the user experience will be better.

The palette of Android5.0 is simple and practical

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.