package com.holim.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ViewFlipper;
// View.OnTouchListener
// CompoundButton.OnCheckedChangeListener
public class MyViewFlipper extends Activity
implements View.OnTouchListener,
CompoundButton.OnCheckedChangeListener {
CheckBox checkBox;
ViewFlipper flipper;
// x
float xAtDown;
float xAtUp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox = (CheckBox)findViewById(R.id.chkAuto);
checkBox.setOnCheckedChangeListener(this);
flipper = (ViewFlipper)findViewById(R.id.viewFlipper);
flipper.setOnTouchListener(this);
// ViewFlipper child view
TextView tv = new TextView(this);
tv.setText("View 4\nDynamically added");
tv.setTextColor(Color.CYAN);
flipper.addView(tv);
}
// View.OnTouchListener abstract method
// flipper
@Override
public boolean onTouch(View v, MotionEvent event) {
// ViewFlipper return
if(v != flipper) return false;
if(event.getAction() == MotionEvent.ACTION_DOWN) {
xAtDown = event.getX(); // x
}
else if(event.getAction() == MotionEvent.ACTION_UP){
xAtUp = event.getX(); // x
if( xAtUp < xAtDown ) {
//
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
// view
flipper.showNext();
}
else if (xAtUp > xAtDown){
//
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
// view
flipper.showPrevious();
}
}
return true;
}
// CompoundButton.OnCheckedChangeListener abstract method
//
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.w("checked", Boolean.toString(isChecked));
if(isChecked == true) {
//
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
// Flipping ( 3)
flipper.setFlipInterval(3000);
flipper.startFlipping();
}
else
{
// Flipping
flipper.stopFlipping();
}
}
}
|