Android折叠效果实现案例

移动开发 Android
为了使界面的效果更加绚丽,体验效果更佳,往往需要开发者们自行开发新的界面效果,在这里,我将奉上各种实现折叠效果的Demo,供大家一同分享。

源码效果地址:https://github.com/openaphid/android-flip.git

废话不多说,直接上代码:

MainActivity.java 

  1.  /* 
  2. Copyright 2012 Aphid Mobile 
  3.  
  4. Licensed under the Apache License, Version 2.0 (the "License"); 
  5. you may not use this file except in compliance with the License. 
  6. You may obtain a copy of the License at 
  7.   
  8.    http://www.apache.org/licenses/LICENSE-2.0 
  9.  
  10. Unless required by applicable law or agreed to in writing, software 
  11. distributed under the License is distributed on an "AS IS" BASIS, 
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13. See the License for the specific language governing permissions and 
  14. limitations under the License. 
  15.  */ 
  16.  
  17. package com.aphidmobile.flip.demo; 
  18.  
  19. import android.app.Activity; 
  20. import android.app.ListActivity; 
  21. import android.content.Intent; 
  22. import android.net.Uri; 
  23. import android.os.Bundle; 
  24. import android.view.*; 
  25. import android.widget.ListView; 
  26. import android.widget.SimpleAdapter; 
  27. import com.aphidmobile.flipview.demo.R; 
  28.  
  29. import java.util.*; 
  30.  
  31. public class MainActivity extends ListActivity { 
  32.     @Override 
  33.     protected void onCreate(Bundle savedInstanceState) { 
  34.         super.onCreate(savedInstanceState); 
  35.         setListAdapter( 
  36.             new SimpleAdapter( 
  37.                 this, getData(), android.R.layout.simple_list_item_1, new String[]{"title"}, new int[]{android.R.id.text1} 
  38.             ) 
  39.         ); 
  40.         getListView().setScrollbarFadingEnabled(false); 
  41.     } 
  42.      
  43.     @Override 
  44.     public boolean onCreateOptionsMenu(Menu menu) { 
  45.         getMenuInflater().inflate(R.menu.main, menu); 
  46.         return true
  47.     } 
  48.  
  49.     @Override 
  50.     public boolean onOptionsItemSelected(MenuItem item) { 
  51.         Intent intent = new Intent( 
  52.             Intent.ACTION_VIEW, 
  53.             Uri.parse("http://openaphid.github.com/"
  54.         ); 
  55.         startActivity(intent); 
  56.  
  57.         return true
  58.     } 
  59.  
  60.     @SuppressWarnings("unchecked"
  61.     @Override 
  62.     protected void onListItemClick(ListView l, View v, int position, long id) { 
  63.         Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position); 
  64.         Intent intent = new Intent(this, (Class<? extends Activity>)map.get("activity")); 
  65.         startActivity(intent); 
  66.     } 
  67.  
  68.     private List<? extends Map<String, ?>> getData() { 
  69.         List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); 
  70.         addItem(data, "TextViews", FlipTextViewActivity.class); 
  71.         addItem(data, "Buttons", FlipButtonActivity.class); 
  72.         addItem(data, "Complex Layouts", FlipComplexLayoutActivity.class); 
  73.         addItem(data, "Async Content", FlipAsyncContentActivity.class); 
  74.         addItem(data, "Event Listener", FlipTextViewAltActivity.class); 
  75.         addItem(data, "Horizontal", FlipHorizontalLayoutActivity.class); 
  76.         addItem(data, "Issue #5", Issue***ctivity.class); 
  77.         addItem(data, "XML Configuration", FlipTextViewXmlActivity.class); 
  78.         addItem(data, "Fragment", FlipFragmentActivity.class); 
  79.         addItem(data, "Dynamic Adapter Size", FlipDynamicAdapterActivity.class); 
  80.          
  81.         return data; 
  82.     } 
  83.  
  84.     private void addItem(List<Map<String, Object>> data, String title, Class<? extends Activity> activityClass) { 
  85.         Map<String, Object> map = new HashMap<String, Object>(); 
  86.         map.put("title", data.size() + ". " + title); 
  87.         map.put("activity", activityClass); 
  88.         data.add(map); 
  89.     } 
  90.   

FlipViewController.java

 

  1. /* 
  2. Copyright 2012 Aphid Mobile 
  3.  
  4. Licensed under the Apache License, Version 2.0 (the "License"); 
  5. you may not use this file except in compliance with the License. 
  6. You may obtain a copy of the License at 
  7.   
  8.    http://www.apache.org/licenses/LICENSE-2.0 
  9.  
  10. Unless required by applicable law or agreed to in writing, software 
  11. distributed under the License is distributed on an "AS IS" BASIS, 
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13. See the License for the specific language governing permissions and 
  14. limitations under the License. 
  15.  
  16.  */ 
  17.  
  18. package com.aphidmobile.flip; 
  19.  
  20. import android.content.Context; 
  21. import android.content.res.Configuration; 
  22. import android.content.res.TypedArray; 
  23. import android.database.DataSetObserver; 
  24. import android.graphics.Bitmap; 
  25. import android.graphics.PixelFormat; 
  26. import android.opengl.GLSurfaceView; 
  27. import android.os.Handler; 
  28. import android.os.Message; 
  29. import android.util.AttributeSet; 
  30. import android.view.*; 
  31. import android.widget.AbsListView; 
  32. import android.widget.Adapter; 
  33. import android.widget.AdapterView; 
  34. import com.aphidmobile.utils.AphidLog; 
  35. import com.openaphid.flip.R; 
  36. import junit.framework.Assert; 
  37.  
  38. import java.util.LinkedList; 
  39.  
  40. public class FlipViewController extends AdapterView<Adapter> { 
  41.  
  42.     public static final int VERTICAL = 0
  43.     public static final int HORIZONTAL = 1
  44.  
  45.     public static interface ViewFlipListener { 
  46.         void onViewFlipped(View view, int position); 
  47.     } 
  48.  
  49.     private static final int MAX_RELEASED_VIEW_SIZE = 1
  50.  
  51.     private static final int MSG_SURFACE_CREATED = 1
  52.  
  53.     private Handler handler = new Handler(new Handler.Callback() { 
  54.         @Override 
  55.         public boolean handleMessage(Message msg) { 
  56.             if (msg.what == MSG_SURFACE_CREATED) { 
  57.                 contentWidth = 0
  58.                 contentHeight = 0
  59.                 requestLayout(); 
  60.                 return true
  61.             } 
  62.             return false
  63.         } 
  64.     }); 
  65.  
  66.     private GLSurfaceView surfaceView; 
  67.     private FlipRenderer renderer; 
  68.     private FlipCards cards; 
  69.  
  70.     private int contentWidth; 
  71.     private int contentHeight; 
  72.  
  73.     @ViewDebug.ExportedProperty 
  74.     private int flipOrientation; 
  75.  
  76.     private boolean inFlipAnimation = false
  77.  
  78.     //AdapterView Related 
  79.     private Adapter adapter; 
  80.  
  81.     private int adapterDataCount = 0
  82.  
  83.     private DataSetObserver adapterDataObserver; 
  84.  
  85.     private final LinkedList<View> bufferedViews = new LinkedList<View>(); 
  86.     private final LinkedList<View> releasedViews = new LinkedList<View>(); //XXX: use a SparseArray to keep the related view indices? 
  87.     private int bufferIndex = -1
  88.     private int adapterIndex = -1
  89.     private int sideBufferSize = 1
  90.  
  91.     private float touchSlop; 
  92.  
  93.     private ViewFlipListener onViewFlipListener; 
  94.  
  95.     @ViewDebug.ExportedProperty 
  96.     private Bitmap.Config animationBitmapFormat = Bitmap.Config.ARGB_8888; 
  97.  
  98.     public FlipViewController(Context context) { 
  99.         this(context, VERTICAL); 
  100.     } 
  101.  
  102.     public FlipViewController(Context context, int flipOrientation) { 
  103.         super(context); 
  104.         init(context, flipOrientation); 
  105.     } 
  106.  
  107.     /** 
  108.      * Constructor required for XML inflation. 
  109.      */ 
  110.     public FlipViewController(Context context, AttributeSet attrs, int defStyle) { 
  111.         super(context, attrs, defStyle); 
  112.  
  113.         int orientation = VERTICAL; 
  114.  
  115.         TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FlipViewController, 00); 
  116.  
  117.         try { 
  118.             int value = a.getInteger(R.styleable.FlipViewController_orientation, VERTICAL); 
  119.             if (value == HORIZONTAL) 
  120.                 orientation = HORIZONTAL; 
  121.  
  122.             value = a.getInteger(R.styleable.FlipViewController_animationBitmapFormat, 0); 
  123.             if (value == 1
  124.                 setAnimationBitmapFormat(Bitmap.Config.ARGB_4444); 
  125.             else if (value == 2
  126.                 setAnimationBitmapFormat(Bitmap.Config.RGB_565); 
  127.             else 
  128.                 setAnimationBitmapFormat(Bitmap.Config.ARGB_8888); 
  129.         } finally { 
  130.             a.recycle(); 
  131.         } 
  132.  
  133.         init(context, orientation); 
  134.     } 
  135.  
  136.     /** 
  137.      * Constructor required for XML inflation. 
  138.      */ 
  139.     public FlipViewController(Context context, AttributeSet attrs) { 
  140.         this(context, attrs, 0); 
  141.     } 
  142.  
  143.     private void init(Context context, int orientation) { 
  144.         ViewConfiguration configuration = ViewConfiguration.get(getContext()); 
  145.         touchSlop = configuration.getScaledTouchSlop(); 
  146.         this.flipOrientation = orientation; 
  147.         setupSurfaceView(context); 
  148.     } 
  149.  
  150.     public Bitmap.Config getAnimationBitmapFormat() { 
  151.         return animationBitmapFormat; 
  152.     } 
  153.  
  154.     /** 
  155.      * Set the bitmap config for the animation, default is ARGB_8888, which provides the best quality with large peak memory consumption. 
  156.      * 
  157.      * @param animationBitmapFormat ALPHA_8 is not supported and will throw exception when binding textures 
  158.      */ 
  159.     public void setAnimationBitmapFormat(Bitmap.Config animationBitmapFormat) { 
  160.         this.animationBitmapFormat = animationBitmapFormat; 
  161.     } 
  162.  
  163.     public ViewFlipListener getOnViewFlipListener() { 
  164.         return onViewFlipListener; 
  165.     } 
  166.  
  167.     public void setOnViewFlipListener(ViewFlipListener onViewFlipListener) { 
  168.         this.onViewFlipListener = onViewFlipListener; 
  169.     } 
  170.  
  171.     public void onResume() { 
  172.         surfaceView.onResume(); 
  173.     } 
  174.  
  175.     public void onPause() { 
  176.         surfaceView.onPause(); 
  177.     } 
  178.  
  179.     /** 
  180.      * Request the animator to update display if the pageView has been preloaded. 
  181.      * <p/> 
  182.      * If the pageView is being used in the animation or its content has been buffered, the animator forcibly reloads it. 
  183.      * <p/> 
  184.      * The reloading process is a bit heavy for an active page, so please don't invoke it too frequently for an active page. The cost is trivial for inactive pages. 
  185.      * 
  186.      * @param pageView 
  187.      */ 
  188.     public void refreshPage(View pageView) { 
  189.         if (cards.refreshPageView(pageView)) 
  190.             requestLayout(); 
  191.     } 
  192.  
  193.     /** 
  194.      * @param pageIndex 
  195.      * @see #refreshPage(android.view.View) 
  196.      */ 
  197.     public void refreshPage(int pageIndex) { 
  198.         if (cards.refreshPage(pageIndex)) 
  199.             requestLayout(); 
  200.     } 
  201.  
  202.     /** 
  203.      * Force the animator reload all preloaded pages 
  204.      */ 
  205.     public void refreshAllPages() { 
  206.         cards.refreshAllPages(); 
  207.         requestLayout(); 
  208.     } 
  209.  
  210.     //-------------------------------------------------------------------------------------------------------------------- 
  211.     // Touch Event 
  212.     @Override 
  213.     public boolean onInterceptTouchEvent(MotionEvent event) { 
  214.         return cards.handleTouchEvent(event, false); 
  215.     } 
  216.  
  217.     @Override 
  218.     public boolean onTouchEvent(MotionEvent event) { 
  219.         return cards.handleTouchEvent(event, true); 
  220.     } 
  221.  
  222.     //-------------------------------------------------------------------------------------------------------------------- 
  223.     // Orientation 
  224.     @Override 
  225.     protected void onConfigurationChanged(Configuration newConfig) { 
  226.         super.onConfigurationChanged(newConfig); 
  227.         //XXX: adds a global layout listener? 
  228.     } 
  229.  
  230.     //-------------------------------------------------------------------------------------------------------------------- 
  231.     // AdapterView<Adapter> 
  232.     @Override 
  233.     public Adapter getAdapter() { 
  234.         return adapter; 
  235.     } 
  236.  
  237.     @Override 
  238.     public void setAdapter(Adapter adapter) { 
  239.         setAdapter(adapter, 0); 
  240.     } 
  241.  
  242.     public void setAdapter(Adapter adapter, int initialPosition) { 
  243.         if (this.adapter != null
  244.             this.adapter.unregisterDataSetObserver(adapterDataObserver); 
  245.  
  246.         Assert.assertNotNull("adapter should not be null", adapter); 
  247.  
  248.         this.adapter = adapter; 
  249.         adapterDataCount = adapter.getCount(); 
  250.  
  251.         adapterDataObserver = new MyDataSetObserver(); 
  252.         this.adapter.registerDataSetObserver(adapterDataObserver); 
  253.         if (adapterDataCount > 0
  254.             setSelection(initialPosition); 
  255.     } 
  256.  
  257.     @Override 
  258.     public View getSelectedView() { 
  259.         return (bufferIndex < bufferedViews.size() && bufferIndex >= 0) ? bufferedViews.get(bufferIndex) : null
  260.     } 
  261.  
  262.     @Override 
  263.     public void setSelection(int position) { 
  264.         if (adapter == null
  265.             return
  266.  
  267.         Assert.assertTrue("Invalid selection position", position >= 0 && position < adapterDataCount); 
  268.  
  269.         releaseViews(); 
  270.  
  271.         View selectedView = viewFromAdapter(position, true); 
  272.         bufferedViews.add(selectedView); 
  273.  
  274.         for (int i = 1; i <= sideBufferSize; i++) { 
  275.             int previous = position - i; 
  276.             int next = position + i; 
  277.  
  278.             if (previous >= 0
  279.                 bufferedViews.addFirst(viewFromAdapter(previous, false)); 
  280.             if (next < adapterDataCount) 
  281.                 bufferedViews.addLast(viewFromAdapter(next, true)); 
  282.         } 
  283.  
  284.         bufferIndex = bufferedViews.indexOf(selectedView); 
  285.         adapterIndex = position; 
  286.  
  287.         requestLayout(); 
  288.         updateVisibleView(inFlipAnimation ? -1 : bufferIndex); 
  289.  
  290.         cards.resetSelection(position, adapterDataCount); 
  291.     } 
  292.  
  293.     @Override 
  294.     public int getSelectedItemPosition() { 
  295.         return adapterIndex; 
  296.     } 
  297.  
  298.     //-------------------------------------------------------------------------------------------------------------------- 
  299.     // Layout 
  300.     @Override 
  301.     protected void onLayout(boolean changed, int l, int t, int r, int b) { 
  302.         if (AphidLog.ENABLE_DEBUG) 
  303.             AphidLog.d("onLayout: %d, %d, %d, %d; child %d", l, t, r, b, bufferedViews.size()); 
  304.  
  305.         for (View child : bufferedViews) 
  306.             child.layout(00, r - l, b - t); 
  307.  
  308.         if (changed || contentWidth == 0) { 
  309.             int w = r - l; 
  310.             int h = b - t; 
  311.             surfaceView.layout(00, w, h); 
  312.  
  313.             if (contentWidth != w || contentHeight != h) { 
  314.                 contentWidth = w; 
  315.                 contentHeight = h; 
  316.             } 
  317.         } 
  318.  
  319.         if (bufferedViews.size() >= 1) { 
  320.             View frontView = bufferedViews.get(bufferIndex); 
  321.             View backView = null
  322.             if (bufferIndex < bufferedViews.size() - 1
  323.                 backView = bufferedViews.get(bufferIndex + 1); 
  324.             renderer.updateTexture(adapterIndex, frontView, backView == null ? -1 : adapterIndex + 1, backView); 
  325.         } 
  326.     } 
  327.  
  328.     @Override 
  329.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  330.         super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  331.  
  332.         for (View child : bufferedViews) 
  333.             child.measure(widthMeasureSpec, heightMeasureSpec); 
  334.  
  335.         surfaceView.measure(widthMeasureSpec, heightMeasureSpec); 
  336.     } 
  337.  
  338.     //-------------------------------------------------------------------------------------------------------------------- 
  339.     //internal exposed properties & methods 
  340.     float getTouchSlop() { 
  341.         return touchSlop; 
  342.     } 
  343.  
  344.     GLSurfaceView getSurfaceView() { 
  345.         return surfaceView; 
  346.     } 
  347.  
  348.     FlipRenderer getRenderer() { 
  349.         return renderer; 
  350.     } 
  351.  
  352.     int getContentWidth() { 
  353.         return contentWidth; 
  354.     } 
  355.  
  356.     int getContentHeight() { 
  357.         return contentHeight; 
  358.     } 
  359.  
  360.     void reloadTexture() { 
  361.         handler.sendMessage(Message.obtain(handler, MSG_SURFACE_CREATED)); 
  362.     } 
  363.  
  364.     //-------------------------------------------------------------------------------------------------------------------- 
  365.     // Internals 
  366.     private void setupSurfaceView(Context context) { 
  367.         surfaceView = new GLSurfaceView(getContext()); 
  368.  
  369.         cards = new FlipCards(this, flipOrientation == VERTICAL); 
  370.         renderer = new FlipRenderer(this, cards); 
  371.  
  372.         surfaceView.setEGLConfigChooser(8888160); 
  373.         surfaceView.setZOrderOnTop(true); 
  374.         surfaceView.setRenderer(renderer); 
  375.         surfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT); 
  376.         surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 
  377.  
  378.         addViewInLayout(surfaceView, -1new AbsListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT), false); 
  379.     } 
  380.  
  381.     private void releaseViews() { 
  382.         for (View view : bufferedViews) 
  383.             releaseView(view); 
  384.         bufferedViews.clear(); 
  385.         bufferIndex = -1
  386.         adapterIndex = -1
  387.     } 
  388.  
  389.     private void releaseView(View view) { 
  390.         Assert.assertNotNull(view); 
  391.         detachViewFromParent(view); 
  392.         addReleasedView(view); 
  393.     } 
  394.  
  395.     private void addReleasedView(View view) { 
  396.         Assert.assertNotNull(view); 
  397.         if (releasedViews.size() < MAX_RELEASED_VIEW_SIZE) 
  398.             releasedViews.add(view); 
  399.     } 
  400.  
  401.     private View viewFromAdapter(int position, boolean addToTop) { 
  402.         Assert.assertNotNull(adapter); 
  403.  
  404.         View releasedView = releasedViews.isEmpty() ? null : releasedViews.removeFirst(); 
  405.  
  406.         View view = adapter.getView(position, releasedView, this); 
  407.         if (releasedView != null && view != releasedView) 
  408.             addReleasedView(releasedView); 
  409.  
  410.         setupAdapterView(view, addToTop, view == releasedView); 
  411.         return view; 
  412.     } 
  413.  
  414.     private void setupAdapterView(View view, boolean addToTop, boolean isReusedView) { 
  415.         LayoutParams params = view.getLayoutParams(); 
  416.         if (params == null) { 
  417.             params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0); 
  418.         } 
  419.  
  420.         if (isReusedView) 
  421.             attachViewToParent(view, addToTop ? 0 : 1, params); 
  422.         else 
  423.             addViewInLayout(view, addToTop ? 0 : 1, params, true); 
  424.     } 
  425.  
  426.     private void updateVisibleView(int index) { 
  427.         /* 
  428.         if (AphidLog.ENABLE_DEBUG) 
  429.             AphidLog.d("Update visible views, index %d, buffered: %d, adapter %d", index, bufferedViews.size(), adapterIndex); 
  430.         */ 
  431.  
  432.         for (int i = 0; i < bufferedViews.size(); i++) 
  433.             bufferedViews.get(i).setVisibility(index == i ? VISIBLE : INVISIBLE); 
  434.     } 
  435.  
  436.     private void debugBufferedViews() { 
  437.         if (AphidLog.ENABLE_DEBUG) 
  438.             AphidLog.d("bufferedViews: %s; buffer index %d, adapter index %d", bufferedViews, bufferIndex, adapterIndex); 
  439.     } 
  440.  
  441.     void postFlippedToView(final int indexInAdapter) { 
  442.         handler.post(new Runnable() { 
  443.             @Override 
  444.             public void run() { 
  445.                 flippedToView(indexInAdapter, true); 
  446.             } 
  447.         }); 
  448.     } 
  449.  
  450.     void flippedToView(final int indexInAdapter, boolean isPost) { 
  451.         if (AphidLog.ENABLE_DEBUG) 
  452.             AphidLog.d("flippedToView: %d, isPost %s", indexInAdapter, isPost); 
  453.  
  454.         debugBufferedViews(); 
  455.  
  456.         if (indexInAdapter >= 0 && indexInAdapter < adapterDataCount) { 
  457.  
  458.             if (indexInAdapter == adapterIndex + 1) { //forward one page 
  459.                 if (adapterIndex < adapterDataCount - 1) { 
  460.                     adapterIndex++; 
  461.                     View old = bufferedViews.get(bufferIndex); 
  462.                     if (bufferIndex > 0
  463.                         releaseView(bufferedViews.removeFirst()); 
  464.                     if (adapterIndex + sideBufferSize < adapterDataCount) 
  465.                         bufferedViews.addLast(viewFromAdapter(adapterIndex + sideBufferSize, true)); 
  466.                     bufferIndex = bufferedViews.indexOf(old) + 1
  467.                     requestLayout(); 
  468.                     updateVisibleView(inFlipAnimation ? -1 : bufferIndex); 
  469.                 } 
  470.             } else if (indexInAdapter == adapterIndex - 1) { 
  471.                 if (adapterIndex > 0) { 
  472.                     adapterIndex--; 
  473.                     View old = bufferedViews.get(bufferIndex); 
  474.                     if (bufferIndex < bufferedViews.size() - 1
  475.                         releaseView(bufferedViews.removeLast()); 
  476.                     if (adapterIndex - sideBufferSize >= 0
  477.                         bufferedViews.addFirst(viewFromAdapter(adapterIndex - sideBufferSize, false)); 
  478.                     bufferIndex = bufferedViews.indexOf(old) - 1
  479.                     requestLayout(); 
  480.                     updateVisibleView(inFlipAnimation ? -1 : bufferIndex); 
  481.                 } 
  482.             } else { 
  483.                 AphidLog.e("Should not happen: indexInAdapter %d, adapterIndex %d", indexInAdapter, adapterIndex); 
  484.             } 
  485.         } else 
  486.             Assert.fail("Invalid indexInAdapter: " + indexInAdapter); 
  487.         //debugBufferedViews(); 
  488.     } 
  489.  
  490.     void showFlipAnimation() { 
  491.         if (!inFlipAnimation) { 
  492.             inFlipAnimation = true
  493.  
  494.             cards.setVisible(true); 
  495.             surfaceView.requestRender(); 
  496.  
  497.             handler.postDelayed(new Runnable() { //use a delayed message to avoid flicker, the perfect solution would be sending a message from the GL thread  
  498.                 public void run() { 
  499.                     if (inFlipAnimation) 
  500.                         updateVisibleView(-1); 
  501.                 } 
  502.             }, 100); 
  503.         } 
  504.     } 
  505.  
  506.     void postHideFlipAnimation() { 
  507.         if (inFlipAnimation) { 
  508.             handler.post(new Runnable() { 
  509.                 @Override 
  510.                 public void run() { 
  511.                     hideFlipAnimation(); 
  512.                 } 
  513.             }); 
  514.         } 
  515.     } 
  516.  
  517.     private void hideFlipAnimation() { 
  518.         if (inFlipAnimation) { 
  519.             inFlipAnimation = false
  520.  
  521.             updateVisibleView(bufferIndex); 
  522.  
  523.             if (onViewFlipListener != null
  524.                 onViewFlipListener.onViewFlipped(bufferedViews.get(bufferIndex), adapterIndex); 
  525.  
  526.             handler.post(new Runnable() { 
  527.                 public void run() { 
  528.                     if (!inFlipAnimation) { 
  529.                         cards.setVisible(false); 
  530.                         surfaceView.requestRender(); //ask OpenGL to clear its display 
  531.                     } 
  532.                 } 
  533.             }); 
  534.         } 
  535.     } 
  536.  
  537.     private void onDataChanged() { 
  538.         adapterDataCount = adapter.getCount(); 
  539.         int activeIndex; 
  540.         if (adapterIndex < 0
  541.             activeIndex = 0
  542.         else 
  543.             activeIndex = Math.min(adapterIndex, adapterDataCount - 1); 
  544.  
  545.         releaseViews(); 
  546.         setSelection(activeIndex); 
  547.     } 
  548.  
  549.     private class MyDataSetObserver extends DataSetObserver { 
  550.         @Override 
  551.         public void onChanged() { 
  552.             onDataChanged(); 
  553.         } 
  554.  
  555.         @Override 
  556.         public void onInvalidated() { 
  557.             onDataChanged(); 
  558.         } 
  559.     } 
    • 效果如下:
    •  

 

责任编辑:张叶青 来源: eoe Android开发者社区
相关推荐

2017-01-22 17:25:55

Android放大镜效果源码分析

2013-06-25 11:21:35

Android开发幻灯片效果Gallery

2010-08-03 11:29:09

Flex全屏

2009-12-25 14:25:39

WPF图标

2010-09-10 09:57:27

DIV样式

2017-02-14 17:29:42

Android毛玻璃虚化效果

2009-08-17 17:15:48

C# 进度条效果

2022-07-28 14:33:32

webviewweb页面

2011-08-10 14:40:23

iPhone动画

2012-04-10 10:41:59

jQueryASP.NET

2012-04-09 10:34:21

jQuery

2010-09-10 14:33:32

SQL循环语句

2016-03-04 10:50:02

ios圆角高效添加

2010-09-25 13:09:14

2011-08-15 14:16:12

2011-04-25 14:45:38

2009-09-01 16:14:06

C#窗口抖动

2013-01-14 17:18:43

Android开发TextView跑马灯效果

2011-05-30 13:23:11

Android 动画

2011-06-21 16:56:39

喷墨一体机评测
点赞
收藏

51CTO技术栈公众号