1 /*
2  * Copyright (C) 2009 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.view;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import com.example.android.apis.R;
22 
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.view.animation.AnimationUtils;
27 import android.view.animation.Animation;
28 import android.view.animation.TranslateAnimation;
29 import android.widget.AdapterView;
30 import android.widget.ArrayAdapter;
31 import android.widget.Spinner;
32 
33 public class Animation3 extends Activity implements AdapterView.OnItemSelectedListener {
34     private static final String[] INTERPOLATORS = {
35             "Accelerate", "Decelerate", "Accelerate/Decelerate",
36             "Anticipate", "Overshoot", "Anticipate/Overshoot",
37             "Bounce"
38     };
39 
40     @Override
onCreate(Bundle savedInstanceState)41     public void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         setContentView(R.layout.animation_3);
44 
45         Spinner s = (Spinner) findViewById(R.id.spinner);
46         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
47                 android.R.layout.simple_spinner_item, INTERPOLATORS);
48         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
49         s.setAdapter(adapter);
50         s.setOnItemSelectedListener(this);
51     }
52 
onItemSelected(AdapterView<?> parent, View v, int position, long id)53     public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
54         final View target = findViewById(R.id.target);
55         final View targetParent = (View) target.getParent();
56 
57         Animation a = new TranslateAnimation(0.0f,
58                 targetParent.getWidth() - target.getWidth() - targetParent.getPaddingLeft() -
59                 targetParent.getPaddingRight(), 0.0f, 0.0f);
60         a.setDuration(1000);
61         a.setStartOffset(300);
62         a.setRepeatMode(Animation.RESTART);
63         a.setRepeatCount(Animation.INFINITE);
64 
65         switch (position) {
66             case 0:
67                 a.setInterpolator(AnimationUtils.loadInterpolator(this,
68                         android.R.anim.accelerate_interpolator));
69                 break;
70             case 1:
71                 a.setInterpolator(AnimationUtils.loadInterpolator(this,
72                         android.R.anim.decelerate_interpolator));
73                 break;
74             case 2:
75                 a.setInterpolator(AnimationUtils.loadInterpolator(this,
76                         android.R.anim.accelerate_decelerate_interpolator));
77                 break;
78             case 3:
79                 a.setInterpolator(AnimationUtils.loadInterpolator(this,
80                         android.R.anim.anticipate_interpolator));
81                 break;
82             case 4:
83                 a.setInterpolator(AnimationUtils.loadInterpolator(this,
84                         android.R.anim.overshoot_interpolator));
85                 break;
86             case 5:
87                 a.setInterpolator(AnimationUtils.loadInterpolator(this,
88                         android.R.anim.anticipate_overshoot_interpolator));
89                 break;
90             case 6:
91                 a.setInterpolator(AnimationUtils.loadInterpolator(this,
92                         android.R.anim.bounce_interpolator));
93                 break;
94         }
95 
96         target.startAnimation(a);
97     }
98 
onNothingSelected(AdapterView<?> parent)99     public void onNothingSelected(AdapterView<?> parent) {
100     }
101 }