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