Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2010 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.apis.animation;
     18 
     19 // Need the following import to get access to the app resources, since this
     20 // class is in a sub-package.
     21 import android.animation.Animator;
     22 import android.animation.ObjectAnimator;
     23 import android.widget.LinearLayout;
     24 import com.example.android.apis.R;
     25 
     26 import android.animation.AnimatorListenerAdapter;
     27 import android.animation.Keyframe;
     28 import android.animation.LayoutTransition;
     29 import android.animation.PropertyValuesHolder;
     30 import android.view.View;
     31 import android.view.ViewGroup;
     32 import android.widget.CheckBox;
     33 import android.widget.CompoundButton;
     34 
     35 import android.app.Activity;
     36 import android.os.Bundle;
     37 import android.widget.Button;
     38 
     39 /**
     40  * This application demonstrates how to use LayoutTransition to automate transition animations
     41  * as items are hidden or shown in a container.
     42  */
     43 public class LayoutAnimationsHideShow extends Activity {
     44 
     45     private int numButtons = 1;
     46     ViewGroup container = null;
     47     private LayoutTransition mTransitioner;
     48 
     49     /** Called when the activity is first created. */
     50     @Override
     51     public void onCreate(Bundle savedInstanceState) {
     52         super.onCreate(savedInstanceState);
     53         setContentView(R.layout.layout_animations_hideshow);
     54 
     55         final CheckBox hideGoneCB = (CheckBox) findViewById(R.id.hideGoneCB);
     56 
     57         container = new LinearLayout(this);
     58         container.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
     59                 ViewGroup.LayoutParams.MATCH_PARENT));
     60 
     61         // Add a slew of buttons to the container. We won't add any more buttons at runtime, but
     62         // will just show/hide the buttons we've already created
     63         for (int i = 0; i < 4; ++i) {
     64             Button newButton = new Button(this);
     65             newButton.setText(String.valueOf(i));
     66             container.addView(newButton);
     67             newButton.setOnClickListener(new View.OnClickListener() {
     68                 public void onClick(View v) {
     69                     v.setVisibility(hideGoneCB.isChecked() ? View.GONE : View.INVISIBLE);
     70                 }
     71             });
     72         }
     73 
     74         resetTransition();
     75 
     76         ViewGroup parent = (ViewGroup) findViewById(R.id.parent);
     77         parent.addView(container);
     78 
     79         Button addButton = (Button) findViewById(R.id.addNewButton);
     80         addButton.setOnClickListener(new View.OnClickListener() {
     81             public void onClick(View v) {
     82                 for (int i = 0; i < container.getChildCount(); ++i) {
     83                     View view = (View) container.getChildAt(i);
     84                     view.setVisibility(View.VISIBLE);
     85                 }
     86             }
     87         });
     88 
     89         CheckBox customAnimCB = (CheckBox) findViewById(R.id.customAnimCB);
     90         customAnimCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     91             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     92                 long duration;
     93                 if (isChecked) {
     94                     mTransitioner.setStagger(LayoutTransition.CHANGE_APPEARING, 30);
     95                     mTransitioner.setStagger(LayoutTransition.CHANGE_DISAPPEARING, 30);
     96                     setupCustomAnimations();
     97                     duration = 500;
     98                 } else {
     99                     resetTransition();
    100                     duration = 300;
    101                 }
    102                 mTransitioner.setDuration(duration);
    103             }
    104         });
    105     }
    106 
    107     private void resetTransition() {
    108         mTransitioner = new LayoutTransition();
    109         container.setLayoutTransition(mTransitioner);
    110     }
    111 
    112     private void setupCustomAnimations() {
    113         // Changing while Adding
    114         PropertyValuesHolder pvhLeft =
    115                 PropertyValuesHolder.ofInt("left", 0, 1);
    116         PropertyValuesHolder pvhTop =
    117                 PropertyValuesHolder.ofInt("top", 0, 1);
    118         PropertyValuesHolder pvhRight =
    119                 PropertyValuesHolder.ofInt("right", 0, 1);
    120         PropertyValuesHolder pvhBottom =
    121                 PropertyValuesHolder.ofInt("bottom", 0, 1);
    122         PropertyValuesHolder pvhScaleX =
    123                 PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
    124         PropertyValuesHolder pvhScaleY =
    125                 PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);
    126         final ObjectAnimator changeIn = ObjectAnimator.ofPropertyValuesHolder(
    127                         this, pvhLeft, pvhTop, pvhRight, pvhBottom, pvhScaleX, pvhScaleY).
    128                 setDuration(mTransitioner.getDuration(LayoutTransition.CHANGE_APPEARING));
    129         mTransitioner.setAnimator(LayoutTransition.CHANGE_APPEARING, changeIn);
    130         changeIn.addListener(new AnimatorListenerAdapter() {
    131             public void onAnimationEnd(Animator anim) {
    132                 View view = (View) ((ObjectAnimator) anim).getTarget();
    133                 view.setScaleX(1f);
    134                 view.setScaleY(1f);
    135             }
    136         });
    137 
    138         // Changing while Removing
    139         Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
    140         Keyframe kf1 = Keyframe.ofFloat(.9999f, 360f);
    141         Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
    142         PropertyValuesHolder pvhRotation =
    143                 PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
    144         final ObjectAnimator changeOut = ObjectAnimator.ofPropertyValuesHolder(
    145                         this, pvhLeft, pvhTop, pvhRight, pvhBottom, pvhRotation).
    146                 setDuration(mTransitioner.getDuration(LayoutTransition.CHANGE_DISAPPEARING));
    147         mTransitioner.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changeOut);
    148         changeOut.addListener(new AnimatorListenerAdapter() {
    149             public void onAnimationEnd(Animator anim) {
    150                 View view = (View) ((ObjectAnimator) anim).getTarget();
    151                 view.setRotation(0f);
    152             }
    153         });
    154 
    155         // Adding
    156         ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "rotationY", 90f, 0f).
    157                 setDuration(mTransitioner.getDuration(LayoutTransition.APPEARING));
    158         mTransitioner.setAnimator(LayoutTransition.APPEARING, animIn);
    159         animIn.addListener(new AnimatorListenerAdapter() {
    160             public void onAnimationEnd(Animator anim) {
    161                 View view = (View) ((ObjectAnimator) anim).getTarget();
    162                 view.setRotationY(0f);
    163             }
    164         });
    165 
    166         // Removing
    167         ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "rotationX", 0f, 90f).
    168                 setDuration(mTransitioner.getDuration(LayoutTransition.DISAPPEARING));
    169         mTransitioner.setAnimator(LayoutTransition.DISAPPEARING, animOut);
    170         animOut.addListener(new AnimatorListenerAdapter() {
    171             public void onAnimationEnd(Animator anim) {
    172                 View view = (View) ((ObjectAnimator) anim).getTarget();
    173                 view.setRotationX(0f);
    174             }
    175         });
    176 
    177     }
    178 }