John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1 | |
| 2 | package com.example.renderthread; |
| 3 | |
| 4 | import android.animation.TimeInterpolator; |
| 5 | import android.app.Activity; |
| 6 | import android.content.Intent; |
| 7 | import android.os.Bundle; |
| 8 | import android.os.Handler; |
| 9 | import android.os.SystemClock; |
| 10 | import android.view.DisplayList; |
| 11 | import android.view.HardwareRenderer; |
| 12 | import android.view.ThreadedRenderer; |
| 13 | import android.view.View; |
| 14 | import android.view.animation.AccelerateDecelerateInterpolator; |
| 15 | import android.widget.AdapterView; |
| 16 | import android.widget.AdapterView.OnItemClickListener; |
| 17 | import android.widget.ListView; |
| 18 | import android.widget.SimpleAdapter; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | public class MainActivity extends Activity implements OnItemClickListener { |
| 25 | |
| 26 | static final int DURATION = 400; |
| 27 | |
| 28 | static final String KEY_NAME = "name"; |
| 29 | static final String KEY_CLASS = "clazz"; |
| 30 | |
| 31 | static Map<String,?> make(String name) { |
| 32 | Map<String,Object> ret = new HashMap<String,Object>(); |
| 33 | ret.put(KEY_NAME, name); |
| 34 | return ret; |
| 35 | } |
| 36 | |
| 37 | @SuppressWarnings("serial") |
| 38 | static final ArrayList<Map<String,?>> SAMPLES = new ArrayList<Map<String,?>>() {{ |
| 39 | for (int i = 1; i < 25; i++) { |
| 40 | add(make("List Item: " + i)); |
| 41 | } |
| 42 | }}; |
| 43 | |
| 44 | Handler mHandler = new Handler(); |
| 45 | |
| 46 | @Override |
| 47 | protected void onCreate(Bundle savedInstanceState) { |
| 48 | super.onCreate(savedInstanceState); |
| 49 | HardwareRenderer.sUseRenderThread = true; |
| 50 | setContentView(R.layout.activity_main); |
| 51 | ListView lv = (ListView) findViewById(android.R.id.list); |
| 52 | lv.setDrawSelectorOnTop(true); |
| 53 | lv.setAdapter(new SimpleAdapter(this, SAMPLES, |
| 54 | R.layout.item_layout, new String[] { KEY_NAME }, |
| 55 | new int[] { android.R.id.text1 })); |
| 56 | lv.setOnItemClickListener(this); |
| 57 | getActionBar().setTitle("MainActivity"); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void onResume() { |
| 62 | super.onResume(); |
| 63 | ListView lv = (ListView) findViewById(android.R.id.list); |
| 64 | for (int i = 0; i < lv.getChildCount(); i++) { |
| 65 | lv.getChildAt(i).animate().translationY(0).setDuration(DURATION); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private static class DisplayListAnimator { |
| 70 | private static final TimeInterpolator sDefaultInterpolator = |
| 71 | new AccelerateDecelerateInterpolator(); |
| 72 | |
| 73 | DisplayList mDisplayList; |
| 74 | float mFromValue; |
| 75 | float mDelta; |
| 76 | long mDuration = DURATION * 2; |
| 77 | long mStartTime; |
| 78 | |
| 79 | DisplayListAnimator(View view, float translateXBy) { |
| 80 | mDelta = translateXBy; |
| 81 | mFromValue = view.getTranslationY(); |
| 82 | mDisplayList = view.getDisplayList(); |
| 83 | } |
| 84 | |
| 85 | boolean animate(long currentTime) { |
| 86 | if (mStartTime == 0) mStartTime = currentTime; |
| 87 | |
| 88 | float fraction = (float)(currentTime - mStartTime) / mDuration; |
| 89 | if (fraction > 1) { |
| 90 | return false; |
| 91 | } |
| 92 | fraction = sDefaultInterpolator.getInterpolation(fraction); |
| 93 | float translation = mFromValue + (mDelta * fraction); |
| 94 | mDisplayList.setTranslationY(translation); |
| 95 | return fraction < 1f; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private static class AnimationExecutor implements Runnable { |
| 100 | DisplayListAnimator[] mAnimations; |
| 101 | ThreadedRenderer mRenderer; |
| 102 | |
| 103 | AnimationExecutor(ThreadedRenderer renderer, DisplayListAnimator[] animations) { |
| 104 | mRenderer = renderer; |
| 105 | mAnimations = animations; |
| 106 | ThreadedRenderer.postToRenderThread(this); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void run() { |
| 111 | boolean hasMore = false; |
| 112 | long now = SystemClock.uptimeMillis(); |
| 113 | for (DisplayListAnimator animator : mAnimations) { |
| 114 | hasMore |= animator.animate(now); |
| 115 | } |
| 116 | mRenderer.repeatLastDraw(); |
| 117 | if (hasMore) { |
| 118 | ThreadedRenderer.postToRenderThread(this); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onItemClick(final AdapterView<?> adapterView, View clickedView, |
| 126 | int clickedPosition, long clickedId) { |
| 127 | int topPosition = adapterView.getFirstVisiblePosition(); |
| 128 | int dy = adapterView.getHeight(); |
| 129 | final DisplayListAnimator[] animators = new DisplayListAnimator[adapterView.getChildCount()]; |
| 130 | for (int i = 0; i < adapterView.getChildCount(); i++) { |
| 131 | int pos = topPosition + i; |
| 132 | View child = adapterView.getChildAt(i); |
| 133 | float delta = (pos - clickedPosition) * 1.1f; |
| 134 | if (delta == 0) delta = -1; |
| 135 | animators[i] = new DisplayListAnimator(child, dy * delta); |
| 136 | } |
| 137 | adapterView.invalidate(); |
| 138 | adapterView.post(new Runnable() { |
| 139 | |
| 140 | @Override |
| 141 | public void run() { |
| 142 | new AnimationExecutor((ThreadedRenderer) adapterView.getHardwareRenderer(), animators); |
| 143 | } |
| 144 | }); |
| 145 | //mHandler.postDelayed(mLaunchActivity, (long) (DURATION * .4)); |
| 146 | mLaunchActivity.run(); |
| 147 | } |
| 148 | |
| 149 | private Runnable mLaunchActivity = new Runnable() { |
| 150 | |
| 151 | @Override |
| 152 | public void run() { |
| 153 | startActivity(new Intent(MainActivity.this, SubActivity.class)); |
| 154 | overridePendingTransition(0, 0); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | } |