Create a Material Design-style Android app-use Drawable and androiddrawable

Source: Internet
Author: User

Create a Material Design-style Android app-use Drawable and androiddrawable

The following Drawables functions help you implement Material Design in applications:

Image resource coloring

In android 5.0 (api 21) and later versions, bitmap and. 9 png can be colored by defining transparency. You can use color resources or topic attributes to parse color resources (for example,?android:attr/colorPrimary). We usually create a resource and then adapt the resource to the topic.

You can color BitmapDrawable or NinePatchDrawable objects.setTint()Method. You can useandroid:tintAndandroid:tintModeSet the color and mode of the color.

Extract highlight from Image

Support library r21 and later versions includePaletteClass to extract the highlighted color from an image. This class can bring up the following highlighted colors:

Vibrant is full of vigor

Vibrant dark is full of life

Vibrant light bright and Vibrant

Muted soft

Muted dark's dark softness

Muted light bright and soft

Pass a Bitmap object to the static method Palette. generate (). It will help you extract the color from the background thread in the background thread. If you cannot use this background thread, use the Palette. generateAsync () method and set a listener.

You can obtain the highlighted color from the image using the getter method in the Palette class, such as Palette. getVibrantColor.

To use the Palette method in a project, you must include the jar of the v7 package palette in the project. The gradle dependecy method is as follows:

?
12 ...compile'com.android.support:palette-v7:21.0.+'

The following is the sample code:

?
123456 Palette.generateAsync(bitmap,newPalette.PaletteAsyncListener() {     publicvoid onGenerated(Palette palette) {         // Do something with colors...         palette.getVibrantColor(Color.BLACK);//get a color in rgb value     } });

For more information, see the Paltette api documentation: http://developer.android.com/reference/android/support/v7/graphics/Palette.html

Create vector drawables

In android 5.0 and later versions, you can create a vector drawable without distortion during scaling. You only need to define a vector image file. On the contrary, to use bitmap, you need to create multiple files for different resolutions. To create a vector image, you must describe the image in detail under the tag of the xml file.

The following is an example:

?
1234567891011121314151617181920 <?xmlversion="1.0"encoding="utf-8"?><!-- res/drawable/heart.xml --><vector    xmlns:android="http://schemas.android.com/apk/res/android"    android:width="256dp"    android:height="256dp"    android:viewportHeight="32"    android:viewportWidth="32">     <!-- draw a path -->    <path        android:fillColor="#f15467"        android:pathData="M20.5,9.5                        c-1.955,0,-3.83,1.268,-4.5,3                        c-0.67,-1.732,-2.547,-3,-4.5,-3                        C8.957,9.5,7,11.432,7,14                        c0,3.53,3.793,6.257,9,11.5                        c5.207,-5.242,9,-7.97,9,-11.5                        C25,11.432,23.043,9.5,20.5,9.5z"/></vector>

The figure above shows the effect as follows:

In android, vector images are displayedVectorDrawableObject. For more information, see Svg Path reference.

References: http://developer.android.com/training/material/drawables.html

Original article address: Workshop.


How to Use createFromXml to create a Drawable instance for android Application Development?

Try
{
XmlPullParser xp = Resources. getSystem (). getXml (R. drawable. g0 );
Drawable db = Drawable. createFromXml (getResources (), xp );
}
Catch (XmlPullParserException e)
{
E. printStackTrace ();
}
Catch (IOException e)
{
E. printStackTrace ();
}

Your error may not be in Activity. getResources () is useful only in activity. Try Content. getResources ()
 
In the Android Application project file, what image resources are placed in the four drawable directories under the res directory? What is the difference?

Different Resolutions.
 

Related Article

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.