Android L Recyclerview, CardView, Palette

Source: Internet
Author: User

Turn:

http://blog.csdn.net/xyz_lmn/article/details/38735117

Material Design mentions that Recyclerview, CardView, and Palette have been added to the Android L version. Recyclerview, CardView are new widgets for displaying complex views. Palette, as a palette class, lets you extract prominent colors from an image.

Recyclerview

Recyclerview as an alternative to the ListView use, Recyclerview standardized viewholder,listview in Convertview is multiplexed, in Recyclerview, is to put Viewholder as the unit of the cache, and then Convertview as the Viewholder member variable remains in the Viewholder, that is, assuming that no screen shows 10 entries, then create 10 Viewholder cache. Viewholder is used every time, so he turns the GetView method into a oncreateviewholder. Viewholder is more suitable for lists of multiple seed layouts, especially for IM conversations. Recyclerview does not provide the Setonitemclicklistener method, you can add events in Viewholder. The use of Recyclerview can be referenced in the Material Design UI Widgets.


Recyclerview can be used for horizontal and vertical sliding views:

Recyclerview 1 Recyclerview 2

Set Landscape:

[Java]View Plaincopyprint?
  1. @Override
  2. protected void OnCreate (Bundle savedinstancestate) {
  3. super.oncreate (savedinstancestate);
  4. Setcontentview (r.layout.activity_recycler_view_horizontal);
  5. //Specify an adapter (see also next example)
  6. list<myadapter.item> itemList = new arraylist<myadapter.item> ();
  7. For (int i = 0; i < ; i++)
  8. Itemlist.add (new Myadapter.item ("Item" + I, "World"));
  9. Madapter = new Myadapter (itemList);
  10. Mrecyclerviewhorizontal = (Recyclerview) Findviewbyid (r.id.my_recycler_view_horizontal);
  11. Mrecyclerviewhorizontal.sethasfixedsize (true);
  12. //Use a linear layout manager
  13. Linearlayoutmanager Mlayoutmanager = new Linearlayoutmanager (this);
  14. Mlayoutmanager.setorientation (linearlayoutmanager.horizontal);
  15. Mrecyclerviewhorizontal.setlayoutmanager (Mlayoutmanager);
  16. Mrecyclerviewhorizontal.setadapter (Madapter);
  17. }



CardView

CardView inherits from the Framelayout class, which allows for consistent display of content in a card layout that can contain fillets and shadows. CardView is a layout that can be used to lay out other view. The use of CardView can be referenced in the Material Design UI Widgets. The article concludes with an example code for this article.

CardView Palette

Palette

If you have tried the SDK for Android Lollipop, you may have noticed the palette. Palette extract the prominent color from the image, so that the color value can be assigned to Actionbar, or other, can make the interface of the whole tone unified.

Creating an Palette instance

There are four ways to create an instance:

1234567891011121314151617181920212223242526 // Synchronous methods.// --------------------------------// These should be used when you have access to the underlying image loading thread.// Picasso allows this through a Transformation. For other libraries, YMMV.// Uses the default palette size (16).Palette p = Palette.generate(bitmap);// Allows you to specify the maximum palette size, in this case 24.Palette p = Palette.generate(bitmap, 24);// Asynchronous methods// --------------------------------// This is the quick and easy integration path. Internally uses an AsyncTask so// this may not be optimal (since you‘re dipping in and out of threads)// Uses the default palette size (16).Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {    @Override    public void onGenerated(Palette palette) {       // Here‘s your generated palette    }});// Allows you to specify the maximum palette size, in this case 24.Palette.generateAsync(bitmap, 24, new Palette.PaletteAsyncListener() {    @Override    public void onGenerated(Palette palette) {       // Here‘s your generated palette    }});

After creating an instance, we also need to get a sample of the samples (Swatch), with 6 samples (swatch):

123456 Vibrant. Palette.getVibrantSwatch()Vibrant dark. Palette.getDarkVibrantSwatch()Vibrant light. Palette.getLightVibrantSwatch()Muted. Palette.getMutedSwatch()Muted dark. Palette.getDarkMutedSwatch()Muted light. Palette.getLightMutedSwatch()

Depending on who you choose, most of the cases we use vibrant and Dark vibrant.

Using Samples (swatch)

Swatch has the following methods:

12345 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, if you want to make the font color and background image match, then use getBodyTextColor() more appropriate, in getTitleTextColor() fact, should and getBodyTextColor() almost.

The following code shows how to extract a color from a picture to set the background color of the TextView to the main color of the picture, and then use getTitleTextColor () to set a matching text color.

123456 Palette.Swatch swatch = palette.getVibrantSwatch();TextView titleView = ...;if(swatch != null) {    titleView.setBackgroundColor(swatch.getRgb());    titleView.setTextColor(swatch.getTitleTextColor());}

It is important to note that getVibrantSwatch() a null value may be returned, so it is necessary to check it.

Problem with size

You can also use the following methods to get all of the swatch at once:

1 List<Palette.Swatch> swatches = palette.getSwatches();

In the above code, you may have noticed that you can set the size of the palette. The larger the size, the longer it takes, and the smaller the color you can choose. The best choice is based on the use of Image:

Avatar and the like, the size of the best between 24-32;

The size of the big picture is almost 8-16;

The default is 16.

===================================================================================================

Palette extract the prominent color from the image, so that the color value can be assigned to Actionbar, or other, can make the interface of the whole tone unified.

The following prominent colors are extracted from the palette class:

Vibrant (energetic)

Vibrant Dark (vibrant dark color)

Vibrant light (vibrant bright color)

Muted (soft)

Muted dark (soft dark color)

Muted light (soft bright color)

Extract the color value code as follows:

[Java]View Plaincopyprint?
  1. Bitmap BM = Bitmapfactory.decoderesource (Getresources (), item.image);
  2. Palette Palette = palette.generate (BM);
  3. if (palette.getlightvibrantcolor () = null) {
  4. Name.setbackgroundcolor (Palette.getlightvibrantcolor (). getRGB ());
  5. Getsupportactionbar (). setbackgrounddrawable (new Colordrawable (Palette.getlightvibrantcolor (). GETRGB ()));
  6. //Getsupportactionbar ().
  7. }



Android L Recyclerview, CardView, Palette

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.