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