MainActivity.java revision ff941dcd815021bb20d6504eb486acb1e50592c3
1cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 2cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckpackage com.example.renderthread; 3cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 4cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.app.Activity; 5cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.content.Intent; 6cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.os.Bundle; 7cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.os.Handler; 8ff941dcd815021bb20d6504eb486acb1e50592c3John Reckimport android.util.Log; 9cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.view.HardwareRenderer; 10e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reckimport android.view.RenderNodeAnimator; 11cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.view.View; 12cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.widget.AdapterView; 13cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.widget.AdapterView.OnItemClickListener; 14cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.widget.ListView; 15cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport android.widget.SimpleAdapter; 16cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 17cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport java.util.ArrayList; 18cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport java.util.HashMap; 19cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckimport java.util.Map; 20cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 21cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckpublic class MainActivity extends Activity implements OnItemClickListener { 22cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 23cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck static final int DURATION = 400; 24cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 25cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck static final String KEY_NAME = "name"; 26cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck static final String KEY_CLASS = "clazz"; 27cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 28cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck static Map<String,?> make(String name) { 29cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck Map<String,Object> ret = new HashMap<String,Object>(); 30cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck ret.put(KEY_NAME, name); 31cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck return ret; 32cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck } 33cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 34cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck @SuppressWarnings("serial") 35cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck static final ArrayList<Map<String,?>> SAMPLES = new ArrayList<Map<String,?>>() {{ 36cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck for (int i = 1; i < 25; i++) { 37cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck add(make("List Item: " + i)); 38cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck } 39cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck }}; 40cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 41cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck Handler mHandler = new Handler(); 42cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 43cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck @Override 44cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck protected void onCreate(Bundle savedInstanceState) { 45cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck super.onCreate(savedInstanceState); 46cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck HardwareRenderer.sUseRenderThread = true; 47cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck setContentView(R.layout.activity_main); 48cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck ListView lv = (ListView) findViewById(android.R.id.list); 49cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck lv.setDrawSelectorOnTop(true); 50cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck lv.setAdapter(new SimpleAdapter(this, SAMPLES, 51cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck R.layout.item_layout, new String[] { KEY_NAME }, 52cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck new int[] { android.R.id.text1 })); 53cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck lv.setOnItemClickListener(this); 54cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck getActionBar().setTitle("MainActivity"); 55cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck } 56cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 57cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck @Override 58cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck protected void onResume() { 59cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck super.onResume(); 60cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck ListView lv = (ListView) findViewById(android.R.id.list); 61cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck for (int i = 0; i < lv.getChildCount(); i++) { 62cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck lv.getChildAt(i).animate().translationY(0).setDuration(DURATION); 63cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck } 64cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck } 65cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 66cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck @Override 67cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck public void onItemClick(final AdapterView<?> adapterView, View clickedView, 68cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck int clickedPosition, long clickedId) { 69cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck int topPosition = adapterView.getFirstVisiblePosition(); 70cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck int dy = adapterView.getHeight(); 71cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck for (int i = 0; i < adapterView.getChildCount(); i++) { 72cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck int pos = topPosition + i; 73cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck View child = adapterView.getChildAt(i); 74cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck float delta = (pos - clickedPosition) * 1.1f; 75cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck if (delta == 0) delta = -1; 76e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck RenderNodeAnimator animator = new RenderNodeAnimator( 77ff941dcd815021bb20d6504eb486acb1e50592c3John Reck RenderNodeAnimator.TRANSLATION_Y, dy * delta); 78e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck animator.setDuration(DURATION); 79ff941dcd815021bb20d6504eb486acb1e50592c3John Reck if (child == clickedView) logTranslationY(clickedView); 80e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck animator.start(child); 81ff941dcd815021bb20d6504eb486acb1e50592c3John Reck if (child == clickedView) logTranslationY(clickedView); 82cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck } 83cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck //mHandler.postDelayed(mLaunchActivity, (long) (DURATION * .4)); 84cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck mLaunchActivity.run(); 85cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck } 86cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 87ff941dcd815021bb20d6504eb486acb1e50592c3John Reck private void logTranslationY(View v) { 88ff941dcd815021bb20d6504eb486acb1e50592c3John Reck Log.d("RTTest", "View has translationY: " + v.getTranslationY()); 89ff941dcd815021bb20d6504eb486acb1e50592c3John Reck } 90ff941dcd815021bb20d6504eb486acb1e50592c3John Reck 91cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck private Runnable mLaunchActivity = new Runnable() { 92cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 93cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck @Override 94cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck public void run() { 95cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck startActivity(new Intent(MainActivity.this, SubActivity.class)); 96cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck overridePendingTransition(0, 0); 97cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck } 98cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck }; 99cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 100cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck} 101