• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.android.calculator2;
18 
19 import android.view.animation.TranslateAnimation;
20 import android.view.MotionEvent;
21 import android.view.View;
22 import android.view.GestureDetector;
23 import android.widget.FrameLayout;
24 import android.content.Context;
25 import android.util.AttributeSet;
26 import android.os.Handler;
27 
28 import java.util.Map;
29 
30 class PanelSwitcher extends FrameLayout {
31     private static final int MAJOR_MOVE = 60;
32     private static final int ANIM_DURATION = 400;
33 
34     private GestureDetector mGestureDetector;
35     private int mCurrentView;
36     private View mChild, mHistoryView;
37     private View children[];
38 
39     private int mWidth;
40     private TranslateAnimation inLeft;
41     private TranslateAnimation outLeft;
42 
43     private TranslateAnimation inRight;
44     private TranslateAnimation outRight;
45 
46     private static final int NONE  = 1;
47     private static final int LEFT  = 2;
48     private static final int RIGHT = 3;
49     private int mPreviousMove;
50 
PanelSwitcher(Context context, AttributeSet attrs)51     public PanelSwitcher(Context context, AttributeSet attrs) {
52         super(context, attrs);
53         mCurrentView = 0;
54         mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
55                 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
56                                        float velocityY) {
57                     int dx = (int) (e2.getX() - e1.getX());
58 
59                     // don't accept the fling if it's too short
60                     // as it may conflict with a button push
61                     if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) {
62                         if (velocityX > 0) {
63                             moveRight();
64                         } else {
65                             moveLeft();
66                         }
67                         return true;
68                     } else {
69                         return false;
70                     }
71                 }
72             });
73     }
74 
75     @Override
onSizeChanged(int w, int h, int oldW, int oldH)76     public void onSizeChanged(int w, int h, int oldW, int oldH) {
77         mWidth = w;
78         inLeft   = new TranslateAnimation(mWidth, 0, 0, 0);
79         outLeft  = new TranslateAnimation(0, -mWidth, 0, 0);
80         inRight  = new TranslateAnimation(-mWidth, 0, 0, 0);
81         outRight = new TranslateAnimation(0, mWidth, 0, 0);
82 
83         inLeft.setDuration(ANIM_DURATION);
84         outLeft.setDuration(ANIM_DURATION);
85         inRight.setDuration(ANIM_DURATION);
86         outRight.setDuration(ANIM_DURATION);
87     }
88 
onFinishInflate()89     protected void onFinishInflate() {
90         int count = getChildCount();
91         children = new View[count];
92         for (int i = 0; i < count; ++i) {
93             children[i] = getChildAt(i);
94             if (i != mCurrentView) {
95                 children[i].setVisibility(View.GONE);
96             }
97         }
98     }
99 
100     @Override
onTouchEvent(MotionEvent event)101     public boolean onTouchEvent(MotionEvent event) {
102         mGestureDetector.onTouchEvent(event);
103         return true;
104     }
105 
106     @Override
onInterceptTouchEvent(MotionEvent event)107     public boolean onInterceptTouchEvent(MotionEvent event) {
108         return mGestureDetector.onTouchEvent(event);
109     }
110 
moveLeft()111     void moveLeft() {
112         //  <--
113         if (mCurrentView < children.length - 1 && mPreviousMove != LEFT) {
114             children[mCurrentView+1].setVisibility(View.VISIBLE);
115             children[mCurrentView+1].startAnimation(inLeft);
116             children[mCurrentView].startAnimation(outLeft);
117             children[mCurrentView].setVisibility(View.GONE);
118 
119             mCurrentView++;
120             mPreviousMove = LEFT;
121         }
122     }
123 
moveRight()124     void moveRight() {
125         //  -->
126         if (mCurrentView > 0 && mPreviousMove != RIGHT) {
127             children[mCurrentView-1].setVisibility(View.VISIBLE);
128             children[mCurrentView-1].startAnimation(inRight);
129             children[mCurrentView].startAnimation(outRight);
130             children[mCurrentView].setVisibility(View.GONE);
131 
132             mCurrentView--;
133             mPreviousMove = RIGHT;
134         }
135     }
136 
getCurrentIndex()137     int getCurrentIndex() {
138         return mCurrentView;
139     }
140 }
141