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