Android UI系列之3D星体旋转效果

来源:互联网 发布:中国人才流失严重知乎 编辑:程序博客网 时间:2024/06/09 20:22

转载自:http://blog.csdn.net/johnwcheung/article/details/52496652

在Android中,如果想要实现3D动画效果一般有两种选择:一是使用Open GL ES,二是使用Camera。Open GL ES使用起来太过复杂,一般是用于比较高级的3D特效或游戏,并且这个也不是开源的,像比较简单的一些3D效果,使用Camera就足够了。

  一些熟知的android 3D动画如对某个View进行旋转或翻转的 Rotate3dAnimation类,还有使用Gallery( Gallery目前已过时,现在都推荐使用 HorizontalScrollView或 RecyclerView替代其实现相应功能) 实现的3D画廊效果等,当然有一些特效要通过伪3D变换来实现,比如CoverFlow效果,它使用标准Android 2D库,还是继承的Gallery类并自定义一些方法,具体实现和使用请参照http://www.cnblogs.com/zealotrouge/p/3380682.html。

  本文要实现的3D星体旋转效果也是从这个CoverFlow演绎而来,不过CoverFlow只是对图像进行转动,我这里要实现的效果是要对所有的View进行类似旋转木马的转动,并且CoverFlow还存在很多已知bug,所以我这里需要重写一些类,并且将Scroller类用Rotator类替代,使界面看起来具有滚动效果,实际上是在转动一组图像。


首先我们需要自定义控件的一些属性,我们将控件取名Carousel,需要设置子项的最小个数和最大个数、当前选中项以及定义旋转角度等,attrs.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <declare-styleable name="Carousel">  
  5.         <attr name="android:gravity" />  
  6.         <attr name="android:animationDuration" />  
  7.         <attr name="UseReflection" format="boolean" />  
  8.         <attr name="Items" format="integer" />  
  9.         <attr name="SelectedItem" format="integer" />  
  10.         <attr name="maxTheta" format="float" />  
  11.         <attr name="minQuantity" format="integer" />  
  12.         <attr name="maxQuantity" format="integer" />  
  13.         <attr name="Names" format="string" />  
  14.     </declare-styleable>  
  15.   
  16. </resources>  


The CarouselImageView Class

这个类装载控件子项在3D空间的位置、子项的索引和当前子项的角度,通过实现Comparable接口,帮助我们确定子项绘制的顺序

[java] view plain copy
  1. package com.john.carousel.lib;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.ImageView;  
  6.   
  7. public class CarouselImageView extends ImageView implements Comparable<CarouselImageView>  
  8. {  
  9.   
  10.     private int index;  
  11.     private float currentAngle;  
  12.     private float x;  
  13.     private float y;  
  14.     private float z;  
  15.     private boolean drawn;  
  16.   
  17.     public CarouselImageView(Context context)  
  18.     {  
  19.         this(context, null0);  
  20.     }  
  21.   
  22.     public CarouselImageView(Context context, AttributeSet attrs)  
  23.     {  
  24.         this(context, attrs, 0);  
  25.     }  
  26.   
  27.     public CarouselImageView(Context context, AttributeSet attrs, int defStyle)  
  28.     {  
  29.         super(context, attrs, defStyle);  
  30.     }  
  31.   
  32.     public void setIndex(int index)  
  33.     {  
  34.         this.index = index;  
  35.     }  
  36.   
  37.     public int getIndex()  
  38.     {  
  39.         return index;  
  40.     }  
  41.   
  42.     public void setCurrentAngle(float currentAngle)  
  43.     {  
  44.         this.currentAngle = currentAngle;  
  45.     }  
  46.   
  47.     public float getCurrentAngle()  
  48.     {  
  49.         return currentAngle;  
  50.     }  
  51.   
  52.     public int compareTo(CarouselImageView another)  
  53.     {  
  54.         return (int) (another.z - this.z);  
  55.     }  
  56.   
  57.     public void setX(float x)  
  58.     {  
  59.         this.x = x;  
  60.     }  
  61.   
  62.     public float getX()  
  63.     {  
  64.         return x;  
  65.     }  
  66.   
  67.     public void setY(float y)  
  68.     {  
  69.         this.y = y;  
  70.     }  
  71.   
  72.     public float getY()  
  73.     {  
  74.         return y;  
  75.     }  
  76.   
  77.     public void setZ(float z)  
  78.     {  
  79.         this.z = z;  
  80.     }  
  81.   
  82.     public float getZ()  
  83.     {  
  84.         return z;  
  85.     }  
  86.   
  87.     public void setDrawn(boolean drawn)  
  88.     {  
  89.         this.drawn = drawn;  
  90.     }  
  91.   
  92.     public boolean isDrawn()  
  93.     {  
  94.         return drawn;  
  95.     }  
  96.   
  97. }  


The Carousel Item Class

这个类简化我上面定义的 CarouselImageView一些控件属性
[java] view plain copy
  1. package com.john.carousel.lib;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Matrix;  
  6. import android.util.Log;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.widget.FrameLayout;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12.   
  13. public class CarouselItem extends FrameLayout implements Comparable<CarouselItem>  
  14. {  
  15.   
  16.     public ImageView mImage;  
  17.     public TextView mText, mTextUp;  
  18.     public Context context;  
  19.     public int index;  
  20.     public float currentAngle;  
  21.     public float itemX;  
  22.     public float itemY;  
  23.     public float itemZ;  
  24.     public float degX;  
  25.     public float degY;  
  26.     public float degZ;  
  27.     public boolean drawn;  
  28.   
  29.     // It's needed to find screen coordinates  
  30.     private Matrix mCIMatrix;  
  31.   
  32.     public CarouselItem(Context context)  
  33.     {  
  34.   
  35.         super(context);  
  36.         this.context = context;  
  37.         FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  38.   
  39.         this.setLayoutParams(params);  
  40.         LayoutInflater inflater = LayoutInflater.from(context);  
  41.         View itemTemplate = inflater.inflate(R.layout.carousel_item, thistrue);  
  42.   
  43.         mImage = (ImageView) itemTemplate.findViewById(R.id.item_image);  
  44.         mText = (TextView) itemTemplate.findViewById(R.id.item_text);  
  45.         mTextUp = (TextView) itemTemplate.findViewById(R.id.item_text_up);  
  46.   
  47.     }  
  48.   
  49.     public void setTextColor(int i)  
  50.     {  
  51.         this.mText.setTextColor(context.getResources().getColorStateList(i));  
  52.         this.mTextUp.setTextColor(context.getResources().getColorStateList(i));  
  53.     }  
  54.   
  55.     public String getName()  
  56.     {  
  57.         return mText.getText().toString();  
  58.     }  
  59.   
  60.     public void setIndex(int index)  
  61.     {  
  62.         this.index = index;  
  63.     }  
  64.   
  65.     public int getIndex()  
  66.     {  
  67.         return index;  
  68.     }  
  69.   
  70.     public void setCurrentAngle(float currentAngle)  
  71.     {  
  72.   
  73.         if (index == 0 && currentAngle > 5)  
  74.         {  
  75.             Log.d("""");  
  76.         }  
  77.   
  78.         this.currentAngle = currentAngle;  
  79.     }  
  80.   
  81.     public float getCurrentAngle()  
  82.     {  
  83.         return currentAngle;  
  84.     }  
  85.   
  86.     public int compareTo(CarouselItem another)  
  87.     {  
  88.         return (int) (another.itemZ - this.itemZ);  
  89.     }  
  90.   
  91.     public void setItemX(float x)  
  92.     {  
  93.         this.itemX = x;  
  94.     }  
  95.   
  96.     public float getItemX()  
  97.     {  
  98.         return itemX;  
  99.     }  
  100.   
  101.     public void setItemY(float y)  
  102.     {  
  103.         this.itemY = y;  
  104.     }  
  105.   
  106.     public float getItemY()  
  107.     {  
  108.         return itemY;  
  109.     }  
  110.   
  111.     public void setItemZ(float z)  
  112.     {  
  113.         this.itemZ = z;  
  114.     }  
  115.   
  116.     public float getItemZ()  
  117.     {  
  118.         return itemZ;  
  119.     }  
  120.   
  121.     public float getDegX()  
  122.     {  
  123.         return degX;  
  124.     }  
  125.   
  126.     public void setDegX(float degX)  
  127.     {  
  128.         this.degX = degX;  
  129.     }  
  130.   
  131.     public float getDegY()  
  132.     {  
  133.         return degY;  
  134.     }  
  135.   
  136.     public void setDegY(float degY)  
  137.     {  
  138.         this.degY = degY;  
  139.     }  
  140.   
  141.     public float getDegZ()  
  142.     {  
  143.         return degZ;  
  144.     }  
  145.   
  146.     public void setDegZ(float degZ)  
  147.     {  
  148.         this.degZ = degZ;  
  149.     }  
  150.   
  151.     public void setDrawn(boolean drawn)  
  152.     {  
  153.         this.drawn = drawn;  
  154.     }  
  155.   
  156.     public boolean isDrawn()  
  157.     {  
  158.         return drawn;  
  159.     }  
  160.   
  161.     public void setImageBitmap(Bitmap bitmap)  
  162.     {  
  163.         mImage.setImageBitmap(bitmap);  
  164.   
  165.     }  
  166.   
  167.     public void setText(int i)  
  168.     {  
  169.         String s = context.getResources().getString(i);  
  170.         mText.setText(s);  
  171.         mTextUp.setText(s);  
  172.     }  
  173.   
  174.     public void setText(String txt)  
  175.     {  
  176.         mText.setText(txt);  
  177.         mTextUp.setText(txt);  
  178.     }  
  179.   
  180.     Matrix getCIMatrix()  
  181.     {  
  182.         return mCIMatrix;  
  183.     }  
  184.   
  185.     void setCIMatrix(Matrix mMatrix)  
  186.     {  
  187.         this.mCIMatrix = mMatrix;  
  188.     }  
  189.   
  190.     public void setImage(int i)  
  191.     {  
  192.         mImage.setImageDrawable(context.getResources().getDrawable(i));  
  193.   
  194.     }  
  195.   
  196.     public void setVisiblity(int id)  
  197.     {  
  198.         if (id == 0)  
  199.         {  
  200.             mText.setVisibility(View.INVISIBLE);  
  201.             mTextUp.setVisibility(View.VISIBLE);  
  202.         }  
  203.         else  
  204.         {  
  205.             mTextUp.setVisibility(View.INVISIBLE);  
  206.             mText.setVisibility(View.VISIBLE);  
  207.         }  
  208.     }  
  209. }  


The Rotator Class

如果你去查看Scroller类方法,你会发现它定义了两种操作模式:滑动模式和抛动作,用来计算当前相对于给出的起始位置的偏移量,我们需要移除一些不需要的成员变量,添加我们自己的成员,并且修改相应的计算方法

[java] view plain copy
  1. package com.john.carousel.lib;  
  2.   
  3. import android.content.Context;  
  4. import android.view.animation.AnimationUtils;  
  5.   
  6. /** 
  7.  * This class encapsulates rotation. The duration of the rotation can be passed 
  8.  * in the constructor and specifies the maximum time that the rotation animation 
  9.  * should take. Past this time, the rotation is automatically moved to its final 
  10.  * stage and computeRotationOffset() will always return false to indicate that 
  11.  * scrolling is over. 
  12.  */  
  13. public class Rotator  
  14. {  
  15.     private float mStartAngle;  
  16.     private float mCurrAngle;  
  17.     private long mStartTime;  
  18.     private long mDuration;  
  19.     private float mDeltaAngle;  
  20.     private boolean mFinished;  
  21.     private int direction;  
  22.     private float mCurrDeg;  
  23.   
  24.     public Rotator(Context context)  
  25.     {  
  26.         mFinished = true;  
  27.     }  
  28.   
  29.     public final boolean isFinished()  
  30.     {  
  31.         return mFinished;  
  32.     }  
  33.   
  34.     /** 
  35.      * Force the finished field to a particular value. 
  36.      * 
  37.      * @param finished 
  38.      *            The new finished value. 
  39.      */  
  40.     public final void forceFinished(boolean finished)  
  41.     {  
  42.         mFinished = finished;  
  43.     }  
  44.   
  45.     /** 
  46.      * Returns how long the scroll event will take, in milliseconds. 
  47.      * 
  48.      * @return The duration of the scroll in milliseconds. 
  49.      */  
  50.     public final long getDuration()  
  51.     {  
  52.         return mDuration;  
  53.     }  
  54.   
  55.     /** 
  56.      * Returns the current X offset in the scroll. 
  57.      * 
  58.      * @return The new X offset as an absolute distance from the origin. 
  59.      */  
  60.     public final float getCurrAngle()  
  61.     {  
  62.         return mCurrAngle;  
  63.     }  
  64.   
  65.     public final float getStartAngle()  
  66.     {  
  67.         return mStartAngle;  
  68.     }  
  69.   
  70.     /** 
  71.      * Returns the time elapsed since the beginning of the scrolling. 
  72.      * 
  73.      * @return The elapsed time in milliseconds. 
  74.      */  
  75.     public int timePassed()  
  76.     {  
  77.         return (int) (AnimationUtils.currentAnimationTimeMillis() - mStartTime);  
  78.     }  
  79.   
  80.     public int getdirection()  
  81.     {  
  82.         return this.direction;  
  83.     }  
  84.   
  85.     public float getCurrDeg()  
  86.     {  
  87.         return this.mCurrDeg;  
  88.     }  
  89.   
  90.     /** 
  91.      * Extend the scroll animation. 
  92.      */  
  93.     public void extendDuration(int extend)  
  94.     {  
  95.         int passed = timePassed();  
  96.         mDuration = passed + extend;  
  97.         mFinished = false;  
  98.     }  
  99.   
  100.     /** 
  101.      * Stops the animation. Contrary to {@link #forceFinished(boolean)}, 
  102.      * aborting the animating cause the scroller to move to the final x and y 
  103.      * position 
  104.      * 
  105.      * @see #forceFinished(boolean) 
  106.      */  
  107.     public void abortAnimation()  
  108.     {  
  109.         mFinished = true;  
  110.     }  
  111.   
  112.     /** 
  113.      * Call this when you want to know the new location. If it returns true, the 
  114.      * animation is not yet finished. loc will be altered to provide the new 
  115.      * location. 
  116.      */  
  117.     public boolean computeAngleOffset()  
  118.     {  
  119.         if (mFinished)  
  120.         {  
  121.             return false;  
  122.         }  
  123.         long systemClock = AnimationUtils.currentAnimationTimeMillis();  
  124.         long timePassed = systemClock - mStartTime;  
  125.         if (timePassed < mDuration)  
  126.         {  
  127.   
  128.             float sc = (float) timePassed / mDuration;  
  129.             mCurrAngle = mStartAngle + Math.round(mDeltaAngle * sc);  
  130.             mCurrDeg = direction == 0 ? (Math.round(360 * sc)) : (Math.round(-360 * sc));  
  131.             return true;  
  132.         }  
  133.         else  
  134.         {  
  135.             mCurrAngle = mStartAngle + mDeltaAngle;  
  136.             mCurrDeg = direction == 0 ? 360 : -360;  
  137.             mFinished = true;  
  138.             return false;  
  139.         }  
  140.     }  
  141.   
  142.     public void startRotate(float startAngle, float dAngle, int duration, int direction)  
  143.     {  
  144.         mFinished = false;  
  145.         mDuration = duration;  
  146.         mStartTime = AnimationUtils.currentAnimationTimeMillis();  
  147.         mStartAngle = startAngle;  
  148.         mDeltaAngle = dAngle;  
  149.         this.direction = direction;  
  150.     }  
  151. }  


The CarouselSpinner Class

[java] view plain copy
  1. package com.john.carousel.lib;  
  2.   
  3. import android.content.Context;  
  4. import android.database.DataSetObserver;  
  5. import android.graphics.Rect;  
  6. import android.os.Parcel;  
  7. import android.os.Parcelable;  
  8. import android.util.AttributeSet;  
  9. import android.util.SparseArray;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.widget.AbsSpinner;  
  13. import android.widget.SpinnerAdapter;  
  14.   
  15. public abstract class CarouselSpinner extends CarouselAdapter<SpinnerAdapter>  
  16. {  
  17.   
  18.     SpinnerAdapter mAdapter;  
  19.   
  20.     int mHeightMeasureSpec;  
  21.     int mWidthMeasureSpec;  
  22.     boolean mBlockLayoutRequests;  
  23.   
  24.     int mSelectionLeftPadding = 0;  
  25.     int mSelectionTopPadding = 0;  
  26.     int mSelectionRightPadding = 0;  
  27.     int mSelectionBottomPadding = 0;  
  28.     final Rect mSpinnerPadding = new Rect();  
  29.   
  30.     final RecycleBin mRecycler = new RecycleBin();  
  31.     private DataSetObserver mDataSetObserver;  
  32.   
  33.     public CarouselSpinner(Context context)  
  34.     {  
  35.         super(context);  
  36.         initCarouselSpinner();  
  37.     }  
  38.   
  39.     public CarouselSpinner(Context context, AttributeSet attrs)  
  40.     {  
  41.         this(context, attrs, 0);  
  42.     }  
  43.   
  44.     public CarouselSpinner(Context context, AttributeSet attrs, int defStyle)  
  45.     {  
  46.         super(context, attrs, defStyle);  
  47.         initCarouselSpinner();  
  48.     }  
  49.   
  50.     /** 
  51.      * Common code for different constructor flavors 
  52.      */  
  53.     private void initCarouselSpinner()  
  54.     {  
  55.         setFocusable(true);  
  56.         setWillNotDraw(false);  
  57.     }  
  58.   
  59.     @Override  
  60.     public SpinnerAdapter getAdapter()  
  61.     {  
  62.         return mAdapter;  
  63.     }  
  64.   
  65.     @Override  
  66.     public void setAdapter(SpinnerAdapter adapter)  
  67.     {  
  68.         if (null != mAdapter)  
  69.         {  
  70.             mAdapter.unregisterDataSetObserver(mDataSetObserver);  
  71.             resetList();  
  72.         }  
  73.   
  74.         mAdapter = adapter;  
  75.   
  76.         mOldSelectedPosition = INVALID_POSITION;  
  77.         mOldSelectedRowId = INVALID_ROW_ID;  
  78.   
  79.         if (mAdapter != null)  
  80.         {  
  81.             mOldItemCount = mItemCount;  
  82.             mItemCount = mAdapter.getCount();  
  83.             checkFocus();  
  84.   
  85.             mDataSetObserver = new AdapterDataSetObserver();  
  86.             mAdapter.registerDataSetObserver(mDataSetObserver);  
  87.   
  88.             int position = mItemCount > 0 ? 0 : INVALID_POSITION;  
  89.   
  90.             setSelectedPositionInt(position);  
  91.             setNextSelectedPositionInt(position);  
  92.   
  93.             if (mItemCount == 0)  
  94.             {  
  95.                 // Nothing selected  
  96.                 checkSelectionChanged();  
  97.             }  
  98.   
  99.         }  
  100.         else  
  101.         {  
  102.             checkFocus();  
  103.             resetList();  
  104.             // Nothing selected  
  105.             checkSelectionChanged();  
  106.         }  
  107.   
  108.         requestLayout();  
  109.   
  110.     }  
  111.   
  112.     @Override  
  113.     public View getSelectedView()  
  114.     {  
  115.         if (mItemCount > 0 && mSelectedPosition >= 0)  
  116.         {  
  117.             return getChildAt(mSelectedPosition - mFirstPosition);  
  118.         }  
  119.         else  
  120.         {  
  121.             return null;  
  122.         }  
  123.     }  
  124.   
  125.     /** 
  126.      * Jump directly to a specific item in the adapter data. 
  127.      */  
  128.     public void setSelection(int position, boolean animate)  
  129.     {  
  130.         // Animate only if requested position is already on screen somewhere  
  131.         boolean shouldAnimate = animate && mFirstPosition <= position && position <= mFirstPosition + getChildCount() - 1;  
  132.         setSelectionInt(position, shouldAnimate);  
  133.     }  
  134.   
  135.     /** 
  136.      * Makes the item at the supplied position selected. 
  137.      * 
  138.      * @param position 
  139.      *            Position to select 
  140.      * @param animate 
  141.      *            Should the transition be animated 
  142.      * 
  143.      */  
  144.     void setSelectionInt(int position, boolean animate)  
  145.     {  
  146.         if (position != mOldSelectedPosition)  
  147.         {  
  148.             mBlockLayoutRequests = true;  
  149.             int delta = position - mSelectedPosition;  
  150.             setNextSelectedPositionInt(position);  
  151.             layout(delta, animate);  
  152.             mBlockLayoutRequests = false;  
  153.         }  
  154.     }  
  155.   
  156.     abstract void layout(int delta, boolean animate);  
  157.   
  158.     @Override  
  159.     public void setSelection(int position)  
  160.     {  
  161.         setSelectionInt(position, false);  
  162.     }  
  163.   
  164.     /** 
  165.      * Clear out all children from the list 
  166.      */  
  167.     void resetList()  
  168.     {  
  169.         mDataChanged = false;  
  170.         mNeedSync = false;  
  171.   
  172.         removeAllViewsInLayout();  
  173.         mOldSelectedPosition = INVALID_POSITION;  
  174.         mOldSelectedRowId = INVALID_ROW_ID;  
  175.   
  176.         setSelectedPositionInt(INVALID_POSITION);  
  177.         setNextSelectedPositionInt(INVALID_POSITION);  
  178.         invalidate();  
  179.     }  
  180.   
  181.     /** 
  182.      * @see android.view.View#measure(int, int) 
  183.      * 
  184.      *      Figure out the dimensions of this Spinner. The width comes from the 
  185.      *      widthMeasureSpec as Spinnners can't have their width set to 
  186.      *      UNSPECIFIED. The height is based on the height of the selected item 
  187.      *      plus padding. 
  188.      */  
  189.     @Override  
  190.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
  191.     {  
  192.         int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
  193.         int widthSize;  
  194.         int heightSize;  
  195.   
  196.         mSpinnerPadding.left = getPaddingLeft() > mSelectionLeftPadding ? getPaddingLeft() : mSelectionLeftPadding;  
  197.         mSpinnerPadding.top = getPaddingTop() > mSelectionTopPadding ? getPaddingTop() : mSelectionTopPadding;  
  198.         mSpinnerPadding.right = getPaddingRight() > mSelectionRightPadding ? getPaddingRight() : mSelectionRightPadding;  
  199.         mSpinnerPadding.bottom = getPaddingBottom() > mSelectionBottomPadding ? getPaddingBottom() : mSelectionBottomPadding;  
  200.   
  201.         if (mDataChanged)  
  202.         {  
  203.             handleDataChanged();  
  204.         }  
  205.   
  206.         int preferredHeight = 0;  
  207.         int preferredWidth = 0;  
  208.         boolean needsMeasuring = true;  
  209.   
  210.         int selectedPosition = getSelectedItemPosition();  
  211.         if (selectedPosition >= 0 && mAdapter != null && selectedPosition < mAdapter.getCount())  
  212.         {  
  213.             // Try looking in the recycler. (Maybe we were measured once  
  214.             // already)  
  215.             View view = mRecycler.get(selectedPosition);  
  216.             if (view == null)  
  217.             {  
  218.                 // Make a new one  
  219.                 view = mAdapter.getView(selectedPosition, nullthis);  
  220.             }  
  221.   
  222.             if (view != null)  
  223.             {  
  224.                 // Put in recycler for re-measuring and/or layout  
  225.                 mRecycler.put(selectedPosition, view);  
  226.             }  
  227.   
  228.             if (view != null)  
  229.             {  
  230.                 if (view.getLayoutParams() == null)  
  231.                 {  
  232.                     mBlockLayoutRequests = true;  
  233.                     view.setLayoutParams(generateDefaultLayoutParams());  
  234.                     mBlockLayoutRequests = false;  
  235.                 }  
  236.                 measureChild(view, widthMeasureSpec, heightMeasureSpec);  
  237.   
  238.                 preferredHeight = getChildHeight(view) + mSpinnerPadding.top + mSpinnerPadding.bottom;  
  239.                 preferredWidth = getChildWidth(view) + mSpinnerPadding.left + mSpinnerPadding.right;  
  240.   
  241.                 needsMeasuring = false;  
  242.             }  
  243.         }  
  244.   
  245.         if (needsMeasuring)  
  246.         {  
  247.             // No views -- just use padding  
  248.             preferredHeight = mSpinnerPadding.top + mSpinnerPadding.bottom;  
  249.             if (widthMode == MeasureSpec.UNSPECIFIED)  
  250.             {  
  251.                 preferredWidth = mSpinnerPadding.left + mSpinnerPadding.right;  
  252.             }  
  253.         }  
  254.   
  255.         preferredHeight = Math.max(preferredHeight, getSuggestedMinimumHeight());  
  256.         preferredWidth = Math.max(preferredWidth, getSuggestedMinimumWidth());  
  257.   
  258.         heightSize = resolveSize(preferredHeight, heightMeasureSpec);  
  259.         widthSize = resolveSize(preferredWidth, widthMeasureSpec);  
  260.   
  261.         setMeasuredDimension(widthSize, heightSize);  
  262.         mHeightMeasureSpec = heightMeasureSpec;  
  263.         mWidthMeasureSpec = widthMeasureSpec;  
  264.     }  
  265.   
  266.     int getChildHeight(View child)  
  267.     {  
  268.         return child.getMeasuredHeight();  
  269.     }  
  270.   
  271.     int getChildWidth(View child)  
  272.     {  
  273.         return child.getMeasuredWidth();  
  274.     }  
  275.   
  276.     @Override  
  277.     protected ViewGroup.LayoutParams generateDefaultLayoutParams()  
  278.     {  
  279.         /* 
  280.          * Carousel expects Carousel.LayoutParams. 
  281.          */  
  282.         return new Carousel.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  283.   
  284.     }  
  285.   
  286.     void recycleAllViews()  
  287.     {  
  288.         final int childCount = getChildCount();  
  289.         final CarouselSpinner.RecycleBin recycleBin = mRecycler;  
  290.         final int position = mFirstPosition;  
  291.   
  292.         // All views go in recycler  
  293.         for (int i = 0; i < childCount; i++)  
  294.         {  
  295.             View v = getChildAt(i);  
  296.             int index = position + i;  
  297.             recycleBin.put(index, v);  
  298.         }  
  299.     }  
  300.   
  301.     /** 
  302.      * Override to prevent spamming ourselves with layout requests as we place 
  303.      * views 
  304.      * 
  305.      * @see android.view.View#requestLayout() 
  306.      */  
  307.     @Override  
  308.     public void requestLayout()  
  309.     {  
  310.         if (!mBlockLayoutRequests)  
  311.         {  
  312.             super.requestLayout();  
  313.         }  
  314.     }  
  315.   
  316.     @Override  
  317.     public int getCount()  
  318.     {  
  319.         return mItemCount;  
  320.     }  
  321.   
  322.     /** 
  323.      * Maps a point to a position in the list. 
  324.      * 
  325.      * @param x 
  326.      *            X in local coordinate 
  327.      * @param y 
  328.      *            Y in local coordinate 
  329.      * @return The position of the item which contains the specified point, or 
  330.      *         {@link #INVALID_POSITION} if the point does not intersect an 
  331.      *         item. 
  332.      */  
  333.     public int pointToPosition(int x, int y)  
  334.     {  
  335.         // All touch events are applied to selected item  
  336.         return mSelectedPosition;  
  337.     }  
  338.   
  339.     static class SavedState extends BaseSavedState  
  340.     {  
  341.         long selectedId;  
  342.         int position;  
  343.   
  344.         /** 
  345.          * Constructor called from {@link AbsSpinner#onSaveInstanceState()} 
  346.          */  
  347.         SavedState(Parcelable superState)  
  348.         {  
  349.             super(superState);  
  350.         }  
  351.   
  352.         /** 
  353.          * Constructor called from {@link #CREATOR} 
  354.          */  
  355.         private SavedState(Parcel in)  
  356.         {  
  357.             super(in);  
  358.             selectedId = in.readLong();  
  359.             position = in.readInt();  
  360.         }  
  361.   
  362.         @Override  
  363.         public void writeToParcel(Parcel out, int flags)  
  364.         {  
  365.             super.writeToParcel(out, flags);  
  366.             out.writeLong(selectedId);  
  367.             out.writeInt(position);  
  368.         }  
  369.   
  370.         @Override  
  371.         public String toString()  
  372.         {  
  373.             return "AbsSpinner.SavedState{" + Integer.toHexString(System.identityHashCode(this)) + " selectedId=" + selectedId + " position=" + position + "}";  
  374.         }  
  375.   
  376.         public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>()  
  377.         {  
  378.             public SavedState createFromParcel(Parcel in)  
  379.             {  
  380.                 return new SavedState(in);  
  381.             }  
  382.   
  383.             public SavedState[] newArray(int size)  
  384.             {  
  385.                 return new SavedState[size];  
  386.             }  
  387.         };  
  388.     }  
  389.   
  390.     @Override  
  391.     public Parcelable onSaveInstanceState()  
  392.     {  
  393.         Parcelable superState = super.onSaveInstanceState();  
  394.         SavedState ss = new SavedState(superState);  
  395.         ss.selectedId = getSelectedItemId();  
  396.         if (ss.selectedId >= 0)  
  397.         {  
  398.             ss.position = getSelectedItemPosition();  
  399.         }  
  400.         else  
  401.         {  
  402.             ss.position = INVALID_POSITION;  
  403.         }  
  404.         return ss;  
  405.     }  
  406.   
  407.     @Override  
  408.     public void onRestoreInstanceState(Parcelable state)  
  409.     {  
  410.         SavedState ss = (SavedState) state;  
  411.   
  412.         super.onRestoreInstanceState(ss.getSuperState());  
  413.   
  414.         if (ss.selectedId >= 0)  
  415.         {  
  416.             mDataChanged = true;  
  417.             mNeedSync = true;  
  418.             mSyncRowId = ss.selectedId;  
  419.             mSyncPosition = ss.position;  
  420.             mSyncMode = SYNC_SELECTED_POSITION;  
  421.             requestLayout();  
  422.         }  
  423.     }  
  424.   
  425.     class RecycleBin  
  426.     {  
  427.         private final SparseArray<View> mScrapHeap = new SparseArray<View>();  
  428.   
  429.         public void put(int position, View v)  
  430.         {  
  431.             mScrapHeap.put(position, v);  
  432.         }  
  433.   
  434.         View get(int position)  
  435.         {  
  436.             // System.out.print("Looking for " + position);  
  437.             View result = mScrapHeap.get(position);  
  438.             if (result != null)  
  439.             {  
  440.                 // System.out.println(" HIT");  
  441.                 mScrapHeap.delete(position);  
  442.             }  
  443.             else  
  444.             {  
  445.                 // System.out.println(" MISS");  
  446.             }  
  447.             return result;  
  448.         }  
  449.   
  450.         void clear()  
  451.         {  
  452.             final SparseArray<View> scrapHeap = mScrapHeap;  
  453.             final int count = scrapHeap.size();  
  454.             for (int i = 0; i < count; i++)  
  455.             {  
  456.                 final View view = scrapHeap.valueAt(i);  
  457.                 if (view != null)  
  458.                 {  
  459.                     removeDetachedView(view, true);  
  460.                 }  
  461.             }  
  462.             scrapHeap.clear();  
  463.         }  
  464.     }  
  465. }  


The CarouselAdapter Class


[The CarouselAdapter vs. AdapterView]
The only changes are in updateEmptyStatus method where unavailable variables were replaced with their getters.


The Carousel Class

[java] view plain copy
  1. package com.john.carousel.lib;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Comparator;  
  6. import android.annotation.SuppressLint;  
  7. import android.content.Context;  
  8. import android.content.res.TypedArray;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.Camera;  
  11. import android.graphics.Matrix;  
  12. import android.graphics.Rect;  
  13. import android.graphics.drawable.BitmapDrawable;  
  14. import android.graphics.drawable.Drawable;  
  15. import android.util.AttributeSet;  
  16. import android.util.Log;  
  17. import android.view.Gravity;  
  18. import android.view.KeyEvent;  
  19. import android.view.View;  
  20. import android.view.ViewGroup;  
  21. import android.view.animation.Transformation;  
  22. import android.widget.BaseAdapter;  
  23.   
  24. public class Carousel extends CarouselSpinner implements Constants  
  25. {  
  26.   
  27.     private int mAnimationDuration = 100;  
  28.     private int mAnimationDurationMin = 50;  
  29.     private Camera mCamera = null;  
  30.     private FlingRotateRunnable mFlingRunnable = null;  
  31.     private int mGravity = 0;  
  32.     private View mSelectedChild = null;  
  33.     private static int mSelectedItemIndex = 2;  
  34.     private boolean mShouldStopFling = false;  
  35.     private static final int LEFT = 0;  
  36.     private static final int RIGHT = 1;  
  37.     /** 
  38.      * If true, do not callback to item selected listener. 
  39.      */  
  40.     private boolean mSuppressSelectionChanged = false;  
  41.     private float mTheta = 0.0f;  
  42.     private boolean isFocus = true;  
  43.   
  44.     private ImageAdapter adapter = null;  
  45.   
  46.     private static final int ONE_ITEM = 1;  
  47.   
  48.     private CarouselItemClickListener callback = null;  
  49.   
  50.     public Carousel(Context context)  
  51.     {  
  52.         this(context, null);  
  53.     }  
  54.   
  55.     public Carousel(Context context, AttributeSet attrs)  
  56.     {  
  57.         this(context, attrs, 0);  
  58.     }  
  59.   
  60.     public Carousel(Context context, AttributeSet attrs, int defStyle)  
  61.     {  
  62.         super(context, attrs, defStyle);  
  63.         setChildrenDrawingOrderEnabled(false);  
  64.         setStaticTransformationsEnabled(true);  
  65.         TypedArray arr = getContext().obtainStyledAttributes(attrs, R.styleable.Carousel);  
  66.         int imageArrayID = arr.getResourceId(R.styleable.Carousel_Items, -1);  
  67.         TypedArray images = getResources().obtainTypedArray(imageArrayID);  
  68.         int namesForItems = arr.getResourceId(R.styleable.Carousel_Names, -1);  
  69.         TypedArray names = null;  
  70.         if (namesForItems != -1)  
  71.         {  
  72.             names = getResources().obtainTypedArray(namesForItems);  
  73.         }  
  74.   
  75.         initView(images, names);  
  76.   
  77.         arr.recycle();  
  78.         images.recycle();  
  79.         if (names != null)  
  80.         {  
  81.             names.recycle();  
  82.         }  
  83.     }  
  84.   
  85.     private void initView(TypedArray images, TypedArray names)  
  86.     {  
  87.         // TODO Auto-generated method stub  
  88.         mCamera = new Camera();  
  89.         mFlingRunnable = new FlingRotateRunnable();  
  90.         mTheta = (float) (15.0f * (Math.PI / 180.0));  
  91.   
  92.         adapter = new ImageAdapter(getContext());  
  93.         adapter.setImages(images, names);  
  94.         setAdapter(adapter);  
  95.         setSelectedPositionInt(mSelectedItemIndex);  
  96.     }  
  97.   
  98.     @Override  
  99.     protected int computeHorizontalScrollExtent()  
  100.     {  
  101.         // Only 1 item is considered to be selected  
  102.         return ONE_ITEM;  
  103.     }  
  104.   
  105.     @Override  
  106.     protected int computeHorizontalScrollOffset()  
  107.     {  
  108.         // Current scroll position is the same as the selected position  
  109.         return mSelectedPosition;  
  110.     }  
  111.   
  112.     @Override  
  113.     protected int computeHorizontalScrollRange()  
  114.     {  
  115.         // Scroll range is the same as the item count  
  116.         return mItemCount;  
  117.     }  
  118.   
  119.     public void setFocusFlag(boolean flag)  
  120.     {  
  121.   
  122.         this.isFocus = flag;  
  123.         adapter.notifyDataSetChanged();  
  124.     }  
  125.   
  126.     public boolean getFocusFlag()  
  127.     {  
  128.         return this.isFocus;  
  129.     }  
  130.   
  131.     public void setSelected(int index)  
  132.     {  
  133.         setNextSelectedPositionInt(index);  
  134.         mSelectedItemIndex = index;  
  135.     }  
  136.   
  137.     public void setCarouselItemClickCallBack(CarouselItemClickListener listener)  
  138.     {  
  139.         callback = listener;  
  140.     }  
  141.   
  142.     public interface CarouselItemClickListener  
  143.     {  
  144.         public void CarouselClickCallBack(int itemPosition);  
  145.     }  
  146.   
  147.     /** 
  148.      * Handles left, right, and clicking 
  149.      *  
  150.      * @see android.view.View#onKeyDown 
  151.      */  
  152.     @Override  
  153.     public boolean onKeyDown(int keyCode, KeyEvent event)  
  154.     {  
  155.         switch (keyCode)  
  156.         {  
  157.         case KEY_OK:  
  158.         case KEY_CENTER:  
  159.             callback.CarouselClickCallBack(mSelectedItemIndex);  
  160.             return true;  
  161.   
  162.         case KEY_LEFT:  
  163.             toNextLeftItem();  
  164.             return true;  
  165.   
  166.         case KEY_RIGHT:  
  167.             toNextRightItem();  
  168.             return true;  
  169.         }  
  170.         return super.onKeyDown(keyCode, event);  
  171.     }  
  172.   
  173.     @Override  
  174.     protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)  
  175.     {  
  176.         super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);  
  177.   
  178.         /* 
  179.          * The gallery shows focus by focusing the selected item. So, give focus 
  180.          * to our selected item instead. We steal keys from our selected item 
  181.          * elsewhere. 
  182.          */  
  183.         if (gainFocus && mSelectedChild != null)  
  184.         {  
  185.             mSelectedChild.requestFocus(direction);  
  186.         }  
  187.   
  188.     }  
  189.   
  190.     @Override  
  191.     protected boolean checkLayoutParams(ViewGroup.LayoutParams p)  
  192.     {  
  193.         return p instanceof LayoutParams;  
  194.     }  
  195.   
  196.     @Override  
  197.     protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)  
  198.     {  
  199.         return new LayoutParams(p);  
  200.     }  
  201.   
  202.     @Override  
  203.     public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)  
  204.     {  
  205.         return new LayoutParams(getContext(), attrs);  
  206.     }  
  207.   
  208.     @Override  
  209.     protected void dispatchSetPressed(boolean pressed)  
  210.     {  
  211.         if (mSelectedChild != null)  
  212.         {  
  213.             mSelectedChild.setPressed(pressed);  
  214.         }  
  215.     }  
  216.   
  217.     @Override  
  218.     public boolean dispatchKeyEvent(KeyEvent event)  
  219.     {  
  220.         return false;  
  221.     }  
  222.   
  223.     /** 
  224.      * Transform an item depending on it's coordinates 
  225.      */  
  226.     @Override  
  227.     protected boolean getChildStaticTransformation(View child, Transformation transformation)  
  228.     {  
  229.   
  230.         transformation.clear();  
  231.         transformation.setTransformationType(Transformation.TYPE_MATRIX);  
  232.         // Center of the view  
  233.         float centerX = (float) getWidth() / 2, centerY = (float) getHeight() / 2;  
  234.         mCamera.save();  
  235.         final Matrix matrix = transformation.getMatrix();  
  236.         mCamera.translate(((CarouselItem) child).getItemX(), ((CarouselItem) child).getItemY(), ((CarouselItem) child).getItemZ());  
  237.         mCamera.getMatrix(matrix);  
  238.         matrix.preTranslate(-centerX, -centerY);  
  239.         matrix.postTranslate(centerX, centerY);  
  240.         float[] values = new float[9];  
  241.         matrix.getValues(values);  
  242.         mCamera.restore();  
  243.         Matrix mm = new Matrix();  
  244.         mm.setValues(values);  
  245.         ((CarouselItem) child).setCIMatrix(mm);  
  246.   
  247.         child.invalidate();  
  248.   
  249.         return true;  
  250.     }  
  251.   
  252.     // CarouselAdapter overrides  
  253.   
  254.     /** 
  255.      * Setting up images 
  256.      */  
  257.     void layout(int delta, boolean animate)  
  258.     {  
  259.         Log.d("ORDER""layout");  
  260.         if (mDataChanged)  
  261.         {  
  262.             handleDataChanged();  
  263.         }  
  264.         if (mNextSelectedPosition >= 0)  
  265.         {  
  266.             setSelectedPositionInt(mNextSelectedPosition);  
  267.         }  
  268.         recycleAllViews();  
  269.         detachAllViewsFromParent();  
  270.         int count = getAdapter().getCount();  
  271.         float angleUnit = 360.0f / count;  
  272.   
  273.         float angleOffset = mSelectedPosition * angleUnit;  
  274.         for (int i = 0; i < getAdapter().getCount(); i++)  
  275.         {  
  276.             float angle = angleUnit * i - angleOffset;  
  277.             if (angle < 0.0f)  
  278.             {  
  279.                 angle = 360.0f + angle;  
  280.             }  
  281.             makeAndAddView(i, angle);  
  282.         }  
  283.         mRecycler.clear();  
  284.         invalidate();  
  285.         setNextSelectedPositionInt(mSelectedPosition);  
  286.         checkSelectionChanged();  
  287.         mNeedSync = false;  
  288.         updateSelectedItemMetadata();  
  289.     }  
  290.   
  291.     /** 
  292.      * Setting up images after layout changed 
  293.      */  
  294.     @Override  
  295.     protected void onLayout(boolean changed, int l, int t, int r, int b)  
  296.     {  
  297.         super.onLayout(changed, l, t, r, b);  
  298.         Log.d("ORDER""onLayout");  
  299.         /* 
  300.          * Remember that we are in layout to prevent more layout request from 
  301.          * being generated. 
  302.          */  
  303.         mInLayout = true;  
  304.         layout(0false);  
  305.         mInLayout = false;  
  306.     }  
  307.   
  308.     @Override  
  309.     void selectionChanged()  
  310.     {  
  311.         if (!mSuppressSelectionChanged)  
  312.         {  
  313.             super.selectionChanged();  
  314.         }  
  315.     }  
  316.   
  317.     @Override  
  318.     void setSelectedPositionInt(int position)  
  319.     {  
  320.         super.setSelectedPositionInt(position);  
  321.         super.setNextSelectedPositionInt(position);  
  322.         updateSelectedItemMetadata();  
  323.     }  
  324.   
  325.     private class FlingRotateRunnable implements Runnable  
  326.     {  
  327.   
  328.         private Rotator mRotator;  
  329.   
  330.         private float mLastFlingAngle;  
  331.   
  332.         public FlingRotateRunnable()  
  333.         {  
  334.             mRotator = new Rotator(getContext());  
  335.         }  
  336.   
  337.         private void startCommon()  
  338.         {  
  339.             removeCallbacks(this);  
  340.         }  
  341.   
  342.         public void startUsingDistance(float deltaAngle, int flag, int direction)  
  343.         {  
  344.             if (deltaAngle == 0)  
  345.                 return;  
  346.             startCommon();  
  347.             mLastFlingAngle = 0;  
  348.   
  349.             synchronized (this)  
  350.             {  
  351.                 mRotator.startRotate(0.0f, -deltaAngle, flag == 0 ? mAnimationDuration : mAnimationDurationMin, direction);  
  352.             }  
  353.             post(this);  
  354.         }  
  355.   
  356.         private void endFling(boolean scrollIntoSlots, int direction)  
  357.         {  
  358.             synchronized (this)  
  359.             {  
  360.                 mRotator.forceFinished(true);  
  361.             }  
  362.             if (scrollIntoSlots)  
  363.             {  
  364.                 scrollIntoSlots(direction);  
  365.             }  
  366.         }  
  367.   
  368.         public void run()  
  369.         {  
  370.             Log.d("ORDER""run");  
  371.             mShouldStopFling = false;  
  372.   
  373.             final Rotator rotator;  
  374.             final float angle;  
  375.             final float deg;  
  376.             boolean more;  
  377.             int direction;  
  378.             synchronized (this)  
  379.             {  
  380.                 rotator = mRotator;  
  381.                 more = rotator.computeAngleOffset();  
  382.                 angle = rotator.getCurrAngle();  
  383.                 deg = rotator.getCurrDeg();  
  384.                 direction = rotator.getdirection();  
  385.             }  
  386.             if (more && !mShouldStopFling)  
  387.             {  
  388.                 Log.d("GETVIEW""========go");  
  389.                 float delta = mLastFlingAngle - angle;  
  390.                 trackMotionScroll(delta, deg);  
  391.                 mLastFlingAngle = angle;  
  392.                 post(this);  
  393.             }  
  394.             else  
  395.             {  
  396.                 Log.d("GETVIEW""========end");  
  397.                 float delta = mLastFlingAngle - angle;  
  398.                 trackMotionScroll(delta, deg);  
  399.                 mLastFlingAngle = 0.0f;  
  400.                 endFling(false, direction);  
  401.             }  
  402.   
  403.         }  
  404.   
  405.     }  
  406.   
  407.     private class ImageAdapter extends BaseAdapter  
  408.     {  
  409.         private Context mContext;  
  410.         private CarouselItem[] mImages;  
  411.   
  412.         private int[] lightImages = { R.drawable.icons_light_network, R.drawable.icons_light_update, R.drawable.icons_light_app, R.drawable.icons_light_stb, R.drawable.icons_light_other,  
  413.                 R.drawable.icons_light_wallpaper, R.drawable.icons_light_media };  
  414.   
  415.         private final int[] normalImages = { R.drawable.icons_normal_network0, R.drawable.icons_normal_update0, R.drawable.icons_normal_app0, R.drawable.icons_normal_stb0,  
  416.                 R.drawable.icons_normal_other0, R.drawable.icons_normal_wallpaper0, R.drawable.icons_normal_meida0 };  
  417.   
  418.         private final int[] colors = { R.color.network_text_color, R.color.update_text_color, R.color.app_text_color, R.color.stb_text_color, R.color.other_text_color, R.color.wallpaper_text_color,  
  419.                 R.color.media_text_color, R.color.text_color_white };  
  420.   
  421.         // private final int[] names = { R.string.STR_NETWORK,  
  422.         // R.string.STR_UPDATE, R.string.STR_APP, R.string.STR_STB,  
  423.         // R.string.STR_OTHER, R.string.STR_WALLPAPER, R.string.STR_MEDIA };  
  424.   
  425.         public ImageAdapter(Context c)  
  426.         {  
  427.             mContext = c;  
  428.         }  
  429.   
  430.         public void setImages(TypedArray array, TypedArray names)  
  431.         {  
  432.             Drawable[] drawables = new Drawable[array.length()];  
  433.             mImages = new CarouselItem[array.length()];  
  434.             for (int i = 0; i < array.length(); i++)  
  435.             {  
  436.                 drawables[i] = array.getDrawable(i);  
  437.                 Bitmap originalImage = ((BitmapDrawable) drawables[i]).getBitmap();  
  438.                 CarouselItem item = new CarouselItem(mContext);  
  439.                 item.setIndex(i);  
  440.                 item.setImageBitmap(originalImage);  
  441.                 if (names != null)  
  442.                 {  
  443.                     item.setText(names.getString(i));  
  444.                 }  
  445.                 if (i == mSelectedItemIndex || (i + 6) % 7 == mSelectedItemIndex || (i + 1) % 7 == mSelectedItemIndex)  
  446.                 {  
  447.                     item.setVisiblity(1);  
  448.                 }  
  449.                 else  
  450.                 {  
  451.                     item.setVisiblity(0);  
  452.                 }  
  453.                 mImages[i] = item;  
  454.             }  
  455.         }  
  456.   
  457.         public int getCount()  
  458.         {  
  459.             if (mImages == null)  
  460.             {  
  461.                 return 0;  
  462.             }  
  463.             else  
  464.             {  
  465.                 return mImages.length;  
  466.             }  
  467.         }  
  468.   
  469.         public Object getItem(int position)  
  470.         {  
  471.             return position;  
  472.         }  
  473.   
  474.         public long getItemId(int position)  
  475.         {  
  476.             return position;  
  477.         }  
  478.   
  479.         public View getView(int position, View convertView, ViewGroup parent)  
  480.         {  
  481.             if (position == mSelectedItemIndex || (position + 6) % 7 == mSelectedItemIndex || (position + 1) % 7 == mSelectedItemIndex)  
  482.             {  
  483.                 mImages[position].setVisiblity(1);  
  484.             }  
  485.             else  
  486.             {  
  487.                 mImages[position].setVisiblity(0);  
  488.             }  
  489.   
  490.             if (position == mSelectedItemIndex && isFocus)  
  491.             {  
  492.                 mImages[position].setImage(lightImages[position]);  
  493.                 mImages[position].setTextColor(colors[position]);  
  494.             }  
  495.             else  
  496.             {  
  497.                 mImages[position].setImage(normalImages[position]);  
  498.                 mImages[position].setTextColor(colors[7]);  
  499.             }  
  500.             Log.d("GETVIEW", position + ":getView");  
  501.   
  502.             return mImages[position];  
  503.         }  
  504.     }  
  505.   
  506.     @SuppressLint("FloatMath")  
  507.     private void Calculate3DPosition(CarouselItem child, int diameter, float angleOffset)  
  508.     {  
  509.         angleOffset = angleOffset * (float) (Math.PI / 180.0f);  
  510.         float x = -(float) (diameter / 2 * android.util.FloatMath.sin(angleOffset) * 1.05) + diameter / 2 - child.getWidth() / 2;  
  511.         float z = diameter / 2 * (1.0f - (float) android.util.FloatMath.cos(angleOffset));  
  512.         float y = -getHeight() / 2 + (float) (z * android.util.FloatMath.sin(mTheta)) + 120;  
  513.         child.setItemX(x);  
  514.         child.setItemZ(z);  
  515.         child.setItemY(y);  
  516.     }  
  517.   
  518.     /** 
  519.      * Figure out vertical placement based on mGravity 
  520.      *  
  521.      * @param child 
  522.      *            Child to place 
  523.      * @return Where the top of the child should be 
  524.      */  
  525.     private int calculateTop(View child, boolean duringLayout)  
  526.     {  
  527.         int myHeight = duringLayout ? getMeasuredHeight() : getHeight();  
  528.         int childHeight = duringLayout ? child.getMeasuredHeight() : child.getHeight();  
  529.   
  530.         int childTop = 0;  
  531.   
  532.         switch (mGravity)  
  533.         {  
  534.         case Gravity.TOP:  
  535.             childTop = mSpinnerPadding.top;  
  536.             break;  
  537.   
  538.         case Gravity.CENTER_VERTICAL:  
  539.             int availableSpace = myHeight - mSpinnerPadding.bottom - mSpinnerPadding.top - childHeight;  
  540.             childTop = mSpinnerPadding.top + (availableSpace / 2);  
  541.             break;  
  542.   
  543.         case Gravity.BOTTOM:  
  544.             childTop = myHeight - mSpinnerPadding.bottom - childHeight;  
  545.             break;  
  546.         }  
  547.   
  548.         return childTop;  
  549.     }  
  550.   
  551.     private void makeAndAddView(int position, float angleOffset)  
  552.     {  
  553.         Log.d("ORDER""makeAndAddView");  
  554.         CarouselItem child;  
  555.   
  556.         if (!mDataChanged)  
  557.         {  
  558.             child = (CarouselItem) mRecycler.get(position);  
  559.             if (child != null)  
  560.             {  
  561.                 // Position the view  
  562.                 setUpChild(child, child.getIndex(), angleOffset);  
  563.             }  
  564.             else  
  565.             {  
  566.                 // Nothing found in the recycler -- ask the adapter for a view  
  567.                 child = (CarouselItem) mAdapter.getView(position, nullthis);  
  568.                 Log.d("GETVIEW""makeAndAddView1");  
  569.                 // Position the view  
  570.                 setUpChild(child, child.getIndex(), angleOffset);  
  571.             }  
  572.             return;  
  573.         }  
  574.   
  575.         // Nothing found in the recycler -- ask the adapter for a view  
  576.         child = (CarouselItem) mAdapter.getView(position, nullthis);  
  577.         Log.d("GETVIEW""makeAndAddView2");  
  578.   
  579.         // Position the view  
  580.         setUpChild(child, child.getIndex(), angleOffset);  
  581.     }  
  582.   
  583.     private void onFinishedMovement()  
  584.     {  
  585.         if (mSuppressSelectionChanged)  
  586.         {  
  587.             mSuppressSelectionChanged = false;  
  588.             super.selectionChanged();  
  589.         }  
  590.         checkSelectionChanged();  
  591.         invalidate();  
  592.     }  
  593.   
  594.     /** 
  595.      * Brings an item with nearest to 0 degrees angle to this angle and sets it 
  596.      * selected 
  597.      */  
  598.     private void scrollIntoSlots(int direction)  
  599.     {  
  600.         Log.d("ORDER""scrollIntoSlots");  
  601.         float angle;  
  602.         int position;  
  603.         ArrayList<CarouselItem> arr = new ArrayList<CarouselItem>();  
  604.         for (int i = 0; i < getAdapter().getCount(); i++)  
  605.         {  
  606.             arr.add(((CarouselItem) getAdapter().getView(i, nullnull)));  
  607.             Log.d("GETVIEW""scrollIntoSlots");  
  608.         }  
  609.         Collections.sort(arr, new Comparator<CarouselItem>()  
  610.         {  
  611.   
  612.             public int compare(CarouselItem c1, CarouselItem c2)  
  613.             {  
  614.                 int a1 = (int) c1.getCurrentAngle();  
  615.                 if (a1 > 180)  
  616.                 {  
  617.                     a1 = 360 - a1;  
  618.                 }  
  619.                 int a2 = (int) c2.getCurrentAngle();  
  620.                 if (a2 > 180)  
  621.                 {  
  622.                     a2 = 360 - a2;  
  623.                 }  
  624.                 return (a1 - a2);  
  625.             }  
  626.         });  
  627.         angle = arr.get(0).getCurrentAngle();  
  628.         if (angle > 180.0f)  
  629.         {  
  630.             angle = -(360.0f - angle);  
  631.         }  
  632.         if (Math.abs(angle) > 0.5f)  
  633.         {  
  634.             mFlingRunnable.startUsingDistance(-angle, 1, direction);  
  635.         }  
  636.         else  
  637.         {  
  638.             position = arr.get(0).getIndex();  
  639.             setSelectedPositionInt(position);  
  640.             onFinishedMovement();  
  641.         }  
  642.     }  
  643.   
  644.     public int getIndex()  
  645.     {  
  646.         return mSelectedItemIndex;  
  647.     }  
  648.   
  649.     private void resetIndex()  
  650.     {  
  651.         if (mSelectedItemIndex == 7)  
  652.         {  
  653.             mSelectedItemIndex = 0;  
  654.         }  
  655.         if (mSelectedItemIndex == -1)  
  656.         {  
  657.             mSelectedItemIndex = 6;  
  658.         }  
  659.     }  
  660.   
  661.     public void toNextRightItem()  
  662.     {  
  663.         mSelectedItemIndex = mSelectedItemIndex - 1;  
  664.         resetIndex();  
  665.         scrollToChild(mSelectedItemIndex, RIGHT);  
  666.         setSelectedPositionInt(mSelectedItemIndex);  
  667.     }  
  668.   
  669.     public void toNextLeftItem()  
  670.     {  
  671.         mSelectedItemIndex = mSelectedItemIndex + 1;  
  672.         resetIndex();  
  673.         scrollToChild(mSelectedItemIndex, LEFT);  
  674.         setSelectedPositionInt(mSelectedItemIndex);  
  675.     }  
  676.   
  677.     void scrollToChild(int i, int v)  
  678.     {  
  679.         Log.d("ORDER""scrollToChild");  
  680.         CarouselItem view = (CarouselItem) getAdapter().getView(i, nullnull);  
  681.         Log.d("GETVIEW""scrollToChild");  
  682.         float angle = view.getCurrentAngle();  
  683.         Log.d("selectCurrentAngle""Angle:" + angle);  
  684.         if (angle == 0)  
  685.         {  
  686.             return;  
  687.         }  
  688.         if (angle > 180.0f)  
  689.         {  
  690.             angle = 360.0f - angle;  
  691.         }  
  692.         else  
  693.         {  
  694.             angle = -angle;  
  695.         }  
  696.         mFlingRunnable.startUsingDistance(angle, 0, v);  
  697.     }  
  698.   
  699.     public void setGravity(int gravity)  
  700.     {  
  701.         if (mGravity != gravity)  
  702.         {  
  703.             mGravity = gravity;  
  704.             requestLayout();  
  705.         }  
  706.     }  
  707.   
  708.     private void setUpChild(CarouselItem child, int index, float angleOffset)  
  709.     {  
  710.         Log.d("ORDER""setUpChild");  
  711.         // Ignore any layout parameters for child, use wrap content  
  712.         addViewInLayout(child, -1 /* index */, generateDefaultLayoutParams());  
  713.         child.setSelected(index == mSelectedPosition);  
  714.         int h;  
  715.         int w;  
  716.         int d;  
  717.         if (mInLayout)  
  718.         {  
  719.             w = child.getMeasuredWidth();  
  720.             h = child.getMeasuredHeight();  
  721.             d = getMeasuredWidth();  
  722.         }  
  723.         else  
  724.         {  
  725.             w = child.getMeasuredWidth();  
  726.             h = child.getMeasuredHeight();  
  727.             d = getWidth();  
  728.         }  
  729.         child.setCurrentAngle(angleOffset);  
  730.         child.measure(w, h);  
  731.         int childLeft;  
  732.         int childTop = calculateTop(child, true);  
  733.         childLeft = 0;  
  734.         child.layout(childLeft, childTop - 45, w, h);  
  735.         Calculate3DPosition(child, d, angleOffset);  
  736.     }  
  737.   
  738.     /** 
  739.      * Tracks a motion scroll. In reality, this is used to do just about any 
  740.      * movement to items (touch scroll, arrow-key scroll, set an item as 
  741.      * selected). 
  742.      */  
  743.     void trackMotionScroll(float deltaAngle, float deg)  
  744.     {  
  745.         Log.d("ORDER""trackMotionScroll");  
  746.         for (int i = 0; i < getAdapter().getCount(); i++)  
  747.         {  
  748.             CarouselItem child = (CarouselItem) getAdapter().getView(i, nullnull);  
  749.             Log.d("GETVIEW""trackMotionScroll");  
  750.             float angle = child.getCurrentAngle();  
  751.             angle += deltaAngle;  
  752.             while (angle > 360.0f)  
  753.             {  
  754.                 angle -= 360.0f;  
  755.             }  
  756.   
  757.             while (angle < 0.0f)  
  758.             {  
  759.                 angle += 360.0f;  
  760.             }  
  761.             child.setCurrentAngle(angle);  
  762.             child.setDegY(deg);  
  763.             Calculate3DPosition(child, getWidth(), angle);  
  764.         }  
  765.         mRecycler.clear();  
  766.         invalidate();  
  767.     }  
  768.   
  769.     private void updateSelectedItemMetadata()  
  770.     {  
  771.   
  772.         View oldSelectedChild = mSelectedChild;  
  773.         View child = mSelectedChild = getChildAt(mSelectedPosition - mFirstPosition);  
  774.         if (child == null)  
  775.         {  
  776.             return;  
  777.         }  
  778.         child.setSelected(true);  
  779.         child.setFocusable(true);  
  780.         if (hasFocus())  
  781.         {  
  782.             child.requestFocus();  
  783.         }  
  784.         if (oldSelectedChild != null)  
  785.         {  
  786.             oldSelectedChild.setSelected(false);  
  787.             oldSelectedChild.setFocusable(false);  
  788.         }  
  789.   
  790.     }  
  791.   
  792. }  


Demo测试类AndroidActivity.Java

[java] view plain copy
  1. package com.john.carousel.test;  
  2.   
  3. import com.john.carousel.lib.Carousel;  
  4. import com.john.carousel.lib.Carousel.CarouselItemClickListener;  
  5. import com.john.carousel.lib.CarouselAdapter;  
  6. import com.john.carousel.lib.CarouselAdapter.cOnItemClickListener;  
  7. import com.john.carousel.lib.Constants;  
  8. import com.john.carousel.lib.R;  
  9.   
  10. import android.app.Activity;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.view.Gravity;  
  14. import android.view.KeyEvent;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.view.View.OnKeyListener;  
  18. import android.widget.LinearLayout;  
  19.   
  20. public class AndroidActivity extends Activity implements CarouselItemClickListener, Constants  
  21. {  
  22.     private Carousel carousel;  
  23.     private final String TAG = AndroidActivity.class.getSimpleName();  
  24.     private LinearLayout layoutMain = null;  
  25.     private final int NETWORK = 0;  
  26.     private final int UPDATE = 1;  
  27.     private final int APK = 2;  
  28.     private final int STB = 3;  
  29.     private final int OTHER = 4;  
  30.     private final int WALLPAPER = 5;  
  31.     private final int MEDIA = 6;  
  32.   
  33.     private int initSelection = 2;  
  34.       
  35.     private long lastClickTime, currClickTime;  
  36.   
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState)  
  39.     {  
  40.         super.onCreate(savedInstanceState);  
  41.         View mainView = LayoutInflater.from(this).inflate(R.layout.activity_android, null);  
  42.         setContentView(mainView);  
  43.   
  44.         initSelection = this.getIntent().getExtras().getInt("selection"2);  
  45.   
  46.         if (initSelection >= 6 || initSelection <= 0)  
  47.         {  
  48.             initSelection = initSelection % 7;  
  49.         }  
  50.   
  51.         buildView();  
  52.   
  53.     }  
  54.   
  55.     private void buildView()  
  56.     {  
  57.         carousel = (Carousel) findViewById(R.id.carousel);  
  58.         layoutMain = (LinearLayout) findViewById(R.id.layoutMain);  
  59.         layoutMain.setBackground(getResources().getDrawable(R.drawable.main_back00));  
  60.         carousel.setDrawingCacheEnabled(true);  
  61.         carousel.setGravity(Gravity.TOP);  
  62.         carousel.setFocusFlag(true);  
  63.         carouselGetFocus();  
  64.         carousel.setSelected(initSelection);  
  65.         carousel.setCarouselItemClickCallBack(this);  
  66.   
  67.         carousel.setOnItemClickListener(new cOnItemClickListener()  
  68.         {  
  69.   
  70.             @Override  
  71.             public void onItemClick(CarouselAdapter<?> parent, View view, int position, long id)  
  72.             {  
  73.                 onItemClickOrCallback(position);  
  74.             }  
  75.         });  
  76.         carousel.setOnKeyListener(new OnKeyListener()  
  77.         {  
  78.   
  79.             @Override  
  80.             public boolean onKey(View v, int keyCode, KeyEvent event)  
  81.             {  
  82.                 if (event.equals(KeyEvent.ACTION_DOWN))  
  83.                 {  
  84.                     switch (keyCode)  
  85.                     {  
  86.                     case KEY_LEFT:  
  87.                         carousel.toNextLeftItem();  
  88.                         break;  
  89.   
  90.                     case KEY_RIGHT:  
  91.                         carousel.toNextRightItem();  
  92.                         break;  
  93.   
  94.                     case KEY_OK:  
  95.                     case KEY_CENTER:  
  96.                         onItemClickOrCallback(carousel.getIndex());  
  97.                         break;  
  98.   
  99.                     }  
  100.                 }  
  101.   
  102.                 carouselGetFocus();  
  103.                 return true;  
  104.             }  
  105.         });  
  106.   
  107.     }  
  108.   
  109.     private void onItemClickOrCallback(int position)  
  110.     {  
  111.   
  112.         switch (position)  
  113.         {  
  114.         case NETWORK:  
  115.   
  116.             break;  
  117.   
  118.         case UPDATE:  
  119.   
  120.             break;  
  121.   
  122.         case APK:  
  123.   
  124.             break;  
  125.   
  126.         case STB:  
  127.   
  128.             break;  
  129.   
  130.         case OTHER:  
  131.   
  132.             break;  
  133.   
  134.         case WALLPAPER:  
  135.   
  136.             break;  
  137.   
  138.         case MEDIA:  
  139.   
  140.             break;  
  141.   
  142.         default:  
  143.             break;  
  144.         }  
  145.     }  
  146.   
  147.     @Override  
  148.     public void CarouselClickCallBack(int itemPosition)  
  149.     {  
  150.         onItemClickOrCallback(itemPosition);  
  151.     }  
  152.   
  153.     @Override  
  154.     public boolean onKeyDown(int keyCode, KeyEvent event)  
  155.     {  
  156.         switch (keyCode)  
  157.         {  
  158.         case KEY_OK:  
  159.         case KEY_CENTER:  
  160.             onItemClickOrCallback(carousel.getIndex());  
  161.             return true;  
  162.   
  163.         case KEY_LEFT:  
  164.             if (carousel.getFocusFlag())  
  165.             {  
  166.                 currClickTime = System.currentTimeMillis();  
  167.                 if (currClickTime - lastClickTime > 200)  
  168.                 {  
  169.                     lastClickTime = currClickTime;  
  170.   
  171.                     carousel.toNextLeftItem();  
  172.                     Log.d("selectedItemIndex", carousel.getIndex() + "");  
  173.                     return true;  
  174.                 }  
  175.                 else  
  176.                 {  
  177.                     return true;  
  178.                 }  
  179.             }  
  180.             break;  
  181.   
  182.         case KEY_RIGHT:  
  183.             if (carousel.getFocusFlag())  
  184.             {  
  185.                 currClickTime = System.currentTimeMillis();  
  186.                 if (currClickTime - lastClickTime > 200)  
  187.                 {  
  188.                     lastClickTime = currClickTime;  
  189.                     carousel.toNextRightItem();  
  190.                     Log.d("selectedItemIndex", carousel.getIndex() + "");  
  191.                     return true;  
  192.                 }  
  193.                 else  
  194.                 {  
  195.                     return true;  
  196.                 }  
  197.             }  
  198.             break;  
  199.   
  200.         case KEY_UP:  
  201.             carousel.setFocusFlag(false);  
  202.             carousel.clearFocus();  
  203.             carousel.setFocusable(false);  
  204.             carousel.setSelected(false);  
  205.   
  206.             return true;  
  207.   
  208.         case KEY_DOWN:  
  209.             if (!carousel.getFocusFlag())  
  210.             {  
  211.                 Log.e(TAG, "KEY_DOWN");  
  212.                 carouselGetFocus();  
  213.             }  
  214.             return true;  
  215.   
  216.         case KEY_EXIT:  
  217.             return true;  
  218.   
  219.         case KEY_VOLDOWN:  
  220.         case KEY_VOLUP:  
  221.         case KEY_MUTE:  
  222.         case KEY_VOLUME_MUTE:  
  223.             return true;  
  224.   
  225.         }  
  226.         return super.onKeyDown(keyCode, event);  
  227.     }  
  228.   
  229.     private void carouselGetFocus()  
  230.     {  
  231.         carousel.setFocusFlag(true);  
  232.         carousel.requestFocus();  
  233.         carousel.setFocusable(true);  
  234.     }  
  235.   
  236. }  

效果展示

http://v.youku.com/v_show/id_XMTcyMDY3ODUxMg==.html