Daniel Olshansky | 5bcd87e | 2013-07-15 09:29:51 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 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.example.android.insertingcells; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.app.Activity; |
| 23 | import android.os.Bundle; |
| 24 | import android.view.View; |
| 25 | import android.widget.Button; |
| 26 | import android.widget.RelativeLayout; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * This application creates a ListView to which new elements can be added from the |
| 33 | * top. When a new element is added, it is animated from above the bounds |
| 34 | * of the list to the top. When the list is scrolled all the way to the top and a new |
| 35 | * element is added, the row animation is accompanied by an image animation that pops |
| 36 | * out of the round view and pops into the correct position in the top cell. |
| 37 | */ |
| 38 | public class InsertingCells extends Activity implements OnRowAdditionAnimationListener { |
| 39 | |
| 40 | private ListItemObject mValues[]; |
| 41 | |
| 42 | private InsertionListView mListView; |
| 43 | |
| 44 | private Button mButton; |
| 45 | |
| 46 | private Integer mItemNum = 0; |
| 47 | |
| 48 | private RoundView mRoundView; |
| 49 | |
| 50 | private int mCellHeight; |
| 51 | |
| 52 | @Override |
| 53 | protected void onCreate(Bundle savedInstanceState) { |
| 54 | super.onCreate(savedInstanceState); |
| 55 | setContentView(R.layout.activity_main); |
| 56 | |
| 57 | mValues = new ListItemObject[] { |
| 58 | new ListItemObject("Chameleon", R.drawable.chameleon, 0), |
| 59 | new ListItemObject("Rock", R.drawable.rock, 0), |
| 60 | new ListItemObject("Flower", R.drawable.flower, 0), |
| 61 | }; |
| 62 | |
| 63 | mCellHeight = (int)(getResources().getDimension(R.dimen.cell_height)); |
| 64 | |
| 65 | List<ListItemObject> mData = new ArrayList<ListItemObject>(); |
| 66 | CustomArrayAdapter mAdapter = new CustomArrayAdapter(this, R.layout.list_view_item, mData); |
| 67 | RelativeLayout mLayout = (RelativeLayout)findViewById(R.id.relative_layout); |
| 68 | |
| 69 | mRoundView = (RoundView)findViewById(R.id.round_view); |
| 70 | mButton = (Button)findViewById(R.id.add_row_button); |
| 71 | mListView = (InsertionListView)findViewById(R.id.listview); |
| 72 | |
| 73 | mListView.setAdapter(mAdapter); |
| 74 | mListView.setData(mData); |
| 75 | mListView.setLayout(mLayout); |
| 76 | mListView.setRowAdditionAnimationListener(this); |
| 77 | } |
| 78 | |
| 79 | public void addRow(View view) { |
| 80 | mButton.setEnabled(false); |
| 81 | |
| 82 | mItemNum++; |
| 83 | ListItemObject obj = mValues[mItemNum % mValues.length]; |
| 84 | final ListItemObject newObj = new ListItemObject(obj.getTitle(), obj.getImgResource(), |
| 85 | mCellHeight); |
| 86 | |
| 87 | boolean shouldAnimateInNewImage = mListView.shouldAnimateInNewImage(); |
| 88 | if (!shouldAnimateInNewImage) { |
| 89 | mListView.addRow(newObj); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | mListView.setEnabled(false); |
| 94 | ObjectAnimator animator = mRoundView.getScalingAnimator(); |
| 95 | animator.addListener(new AnimatorListenerAdapter() { |
| 96 | @Override |
| 97 | public void onAnimationRepeat(Animator animation) { |
| 98 | mListView.addRow(newObj); |
| 99 | } |
| 100 | }); |
| 101 | animator.start(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onRowAdditionAnimationStart() { |
| 106 | mButton.setEnabled(false); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void onRowAdditionAnimationEnd() { |
| 111 | mButton.setEnabled(true); |
| 112 | } |
| 113 | } |