package course.examples.touch.viewtransitions;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class ViewFlipperTestActivity extends Activity {
private ViewFlipper mFlipper;
private TextView mTextView1, mTextView2;
private int mCurrentLayoutState, mCount;
private GestureDetector mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCurrentLayoutState = 0;
mFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
mTextView1 = (TextView) findViewById(R.id.textView1);
mTextView2 = (TextView) findViewById(R.id.textView2);
mTextView1.setText(String.valueOf(mCount));
mGestureDetector = new GestureDetector(this,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
if (velocityX < -10.0f) {
mCurrentLayoutState = mCurrentLayoutState == 0 ? 1
: 0;
switchLayoutStateTo(mCurrentLayoutState);
}
return true;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
public void switchLayoutStateTo(int switchTo) {
mCurrentLayoutState = switchTo;
mFlipper.setInAnimation(inFromRightAnimation());
mFlipper.setOutAnimation(outToLeftAnimation());
mCount++;
if (switchTo == 0) {
mTextView1.setText(String.valueOf(mCount));
} else {
mTextView2.setText(String.valueOf(mCount));
}
mFlipper.showPrevious();
}
private Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
inFromRight.setInterpolator(new LinearInterpolator());
return inFromRight;
}
private Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new LinearInterpolator());
return outtoLeft;
}
}