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.AnimatorListenerAdapter;
     22 import android.animation.Animator;
     23 import android.animation.ObjectAnimator;
     24 import android.view.animation.AccelerateInterpolator;
     25 import android.view.animation.DecelerateInterpolator;
     26 import android.view.animation.Interpolator;
     27 import android.widget.ArrayAdapter;
     28 import android.widget.ListView;
     29 import com.example.android.apis.R;
     30 
     31 import android.app.Activity;
     32 import android.os.Bundle;
     33 import android.view.View;
     34 import android.widget.Button;
     35 import android.widget.SeekBar;
     36 
     37 /**
     38  * This application demonstrates the seeking capability of ValueAnimator. The SeekBar in the
     39  * UI allows you to set the position of the animation. Pressing the Run button will play from
     40  * the current position of the animation.
     41  */
     42 public class ListFlipper extends Activity {
     43 
     44     private static final int DURATION = 1500;
     45     private SeekBar mSeekBar;
     46 
     47     private static final String[] LIST_STRINGS_EN = new String[] {
     48             "One",
     49             "Two",
     50             "Three",
     51             "Four",
     52             "Five",
     53             "Six"
     54     };
     55     private static final String[] LIST_STRINGS_FR = new String[] {
     56             "Un",
     57             "Deux",
     58             "Trois",
     59             "Quatre",
     60             "Le Five",
     61             "Six"
     62     };
     63 
     64     ListView mEnglishList;
     65     ListView mFrenchList;
     66 
     67     /** Called when the activity is first created. */
     68     @Override
     69     public void onCreate(Bundle savedInstanceState) {
     70         super.onCreate(savedInstanceState);
     71         setContentView(R.layout.rotating_list);
     72         //FrameLayout container = (LinearLayout) findViewById(R.id.container);
     73         mEnglishList = (ListView) findViewById(R.id.list_en);
     74         mFrenchList = (ListView) findViewById(R.id.list_fr);
     75 
     76         // Prepare the ListView
     77         final ArrayAdapter<String> adapterEn = new ArrayAdapter<String>(this,
     78                 android.R.layout.simple_list_item_1, LIST_STRINGS_EN);
     79         // Prepare the ListView
     80         final ArrayAdapter<String> adapterFr = new ArrayAdapter<String>(this,
     81                 android.R.layout.simple_list_item_1, LIST_STRINGS_FR);
     82 
     83         mEnglishList.setAdapter(adapterEn);
     84         mFrenchList.setAdapter(adapterFr);
     85         mFrenchList.setRotationY(-90f);
     86 
     87         Button starter = (Button) findViewById(R.id.button);
     88         starter.setOnClickListener(new View.OnClickListener() {
     89             public void onClick(View v) {
     90                 flipit();
     91             }
     92         });
     93     }
     94 
     95     private Interpolator accelerator = new AccelerateInterpolator();
     96     private Interpolator decelerator = new DecelerateInterpolator();
     97     private void flipit() {
     98         final ListView visibleList;
     99         final ListView invisibleList;
    100         if (mEnglishList.getVisibility() == View.GONE) {
    101             visibleList = mFrenchList;
    102             invisibleList = mEnglishList;
    103         } else {
    104             invisibleList = mFrenchList;
    105             visibleList = mEnglishList;
    106         }
    107         ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visibleList, "rotationY", 0f, 90f);
    108         visToInvis.setDuration(500);
    109         visToInvis.setInterpolator(accelerator);
    110         final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisibleList, "rotationY",
    111                 -90f, 0f);
    112         invisToVis.setDuration(500);
    113         invisToVis.setInterpolator(decelerator);
    114         visToInvis.addListener(new AnimatorListenerAdapter() {
    115             @Override
    116             public void onAnimationEnd(Animator anim) {
    117                 visibleList.setVisibility(View.GONE);
    118                 invisToVis.start();
    119                 invisibleList.setVisibility(View.VISIBLE);
    120             }
    121         });
    122         visToInvis.start();
    123     }
    124 
    125 
    126 }