[Excerpt] How do you change the drawing order of views in Android?

Source: Internet
Author: User

Recently I was working on a widget that extends Android's gallery class, which is typically used to show a horizontal list of images that the user can scroll through. I wanted to customize the gallery to actually give a stacking effect to the images, by tucking parts of each image underneath the image next to it, while showing the image in the center on top of everything. (see any "cover flow" images from Google Images)

Luckily Android makes this very easy to accomplish with the method getchilddrawingorder (INT, INT ). the gallery class actually already uses this method to make sure the center image is drawn last, but it draws the rest of the images from left to right, while I actually want to draw the outside images first, then the second and second to last images, etc.

To begin I overrode the getchilddrawingorder () in my class that extends Gallery. also it may be necessary to call setchildrendrawingorderenabled (true) on your view to get the hook into getchilddrawingorder (), but since the gallery already implemented it I did not need. the arguments passed into getchilddrawingorder (), childcount and I are the amount of children of this view and the current drawing index respectively. what you are supposed to return is the index of the child that you want to be drawn on this drawing turn.

If I have 5 images (2 on the left, 1 in the center, and 2 on the right) then the Child Index order I want to give back is 0, 1, 4, 3, 2 to get the effect that I want. for This widget I will always have an odd number of items in my gallery on the screen (3, 5, or 7) So I cocould make some assumptions in my code. as you will see in my code sample below, I solved this very simply with three if statements.

1. If our drawing index is the last index, then return the center child to be drawn, ensuring it will be drawn last.

2. if our drawing index is greater than the center child's index, meaning this item is on the right side of the Center item then return the last possible child index that has not already been drawn yet.

3. else we will just return the same child index that corresponds to the drawing index, because the items on the left side can be drawn in order.

1 @ Override
2 Protected   Int Getchilddrawingorder ( Int Childcount, Int I ){
3 // Reset the last position variable everytime we are starting a new drawing Loop
4 If (I =   0 )
5 Lastposition =   0 ;
6  
7 Int Centerposition = Getselecteditemposition () - Getfirstvisibleposition ();
8  
9 If (I = Childcount -   1 ){
10 Return Centerposition;
11 } Else   If (I > = Centerposition ){
12 Lastposition ++ ;
13 Return Childcount - Lastposition;
14 } Else {
15 Return I;
16 }
17 }

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.