package com.example.animation;

import com.example.animation.view.Rotate3D;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
	int[] imageIds = new int[] { R.drawable.title1, R.drawable.tilte2,
			R.drawable.title3, R.drawable.title4 };
	View[] views = new View[4];
	ImageSwitcher imswitcher;
	GestureDetector mGestureDetector;
	int i = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imswitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
		
		int[] Rid=new int[]{R.id.view1,R.id.view2,R.id.view3,R.id.view4};
		for (int i = 0; i < Rid.length; i++) {
			views[i]=findViewById(Rid[i]);
		}
		
		imswitcher.setFactory(new ViewFactory() {
			@Override
			public View makeView() {
				ImageView imageView = new ImageView(MainActivity.this);
				imageView.setBackgroundColor(0xff0000);
				imageView.setScaleType(ImageView.ScaleType.FIT_START);
				imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				return imageView;
			}
		});
		imswitcher.setImageResource(R.drawable.title1);

		mGestureDetector = new GestureDetector(this, new MyGestureListener());
		imswitcher.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				mGestureDetector.onTouchEvent(event);
				return true;
			}
		});

		DownloadTask dTask = new DownloadTask();
		dTask.execute(100);
	}

	public void setpic(int m) {

		for (int i = 0; i < views.length; i++) {
			if (i == m) {
				views[i].setBackgroundColor(0xffb50202);
			} else {
				views[i].setBackgroundColor(0xffebeaea);
			}
		}
	}
/////////////////////////---------------------
	private class MyGestureListener implements
			GestureDetector.OnGestureListener {

		@Override
		public boolean onDown(MotionEvent e) {
			return false;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if (velocityX > 0) {
				// Toast.makeText(getApplicationContext(), "速度大于0",
				// Toast.LENGTH_SHORT).show();

				float halfWidth = imswitcher.getWidth() / 2.0f;
				float halfHeight = imswitcher.getHeight() / 2.0f;
				int duration = 500;
				int depthz = 0;// viewFlipper.getWidth()/2;

				Rotate3D rdin = new Rotate3D(-75, 0, 0, halfWidth, halfHeight);
				rdin.setDuration(duration);
				rdin.setFillAfter(true);
				imswitcher.setInAnimation(rdin);
				Rotate3D rdout = new Rotate3D(15, 90, 0, halfWidth, halfHeight);

				rdout.setDuration(duration);
				rdout.setFillAfter(true);
				imswitcher.setOutAnimation(rdout);

				i = (i - 1);

				int p = i % 4;
				Log.i("p的值", String.valueOf(p));
				if (p >= 0) {
					setpic(p);
					imswitcher.setImageResource(imageIds[p]);
				} else {
					int k = 4 + p;
					setpic(k);
					imswitcher.setImageResource(imageIds[k]);
				}
			}
			if (velocityX < 0) {
				// Toast.makeText(getApplicationContext(), "速度小于0",
				// Toast.LENGTH_SHORT).show();

				float halfWidth = imswitcher.getWidth() / 2.0f;
				float halfHeight = imswitcher.getHeight() / 2.0f;
				int duration = 500;
				int depthz = 0;// viewFlipper.getWidth()/2;

				Rotate3D rdin = new Rotate3D(75, 0, 0, halfWidth, halfHeight);
				rdin.setDuration(duration);
				rdin.setFillAfter(true);
				imswitcher.setInAnimation(rdin);
				Rotate3D rdout = new Rotate3D(-15, -90, 0, halfWidth,
						halfHeight);

				rdout.setDuration(duration);
				rdout.setFillAfter(true);
				imswitcher.setOutAnimation(rdout);

				i = (i + 1);
				int p = i % 4;
				if (p >= 0) {
					setpic(p);
					imswitcher.setImageResource(imageIds[p]);
				} else {
					int k = 4 + p;
					setpic(k);
					imswitcher.setImageResource(imageIds[k]);

				}
			}
			return true;
		}

		@Override
		public void onLongPress(MotionEvent e) {
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			int p = i % 4;
			if (p >= 0) {

				Toast.makeText(getApplicationContext(), String.valueOf(p),
						Toast.LENGTH_SHORT).show();
			} else {

				int k = 4 + p;
				Toast.makeText(getApplicationContext(), String.valueOf(k),
						Toast.LENGTH_SHORT).show();
			}
			// Toast.makeText(getApplicationContext(), "ssssss",
			// Toast.LENGTH_SHORT).show();

			return true;
		}

	}
//////////------------------------------------------------
	class DownloadTask extends AsyncTask {

		@Override
		protected Object doInBackground(Object... arg0) {
			// TODO Auto-generated method stub

			for (int i = 0; i < Integer.MAX_VALUE; i++) {
				try {
					Thread.sleep(6000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				publishProgress(null);

			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);

			float halfWidth = imswitcher.getWidth() / 2.0f;
			float halfHeight = imswitcher.getHeight() / 2.0f;
			int duration = 500;
			int depthz = 0;// viewFlipper.getWidth()/2;

			Rotate3D rdin = new Rotate3D(75, 0, 0, halfWidth, halfHeight);
			rdin.setDuration(duration);
			rdin.setFillAfter(true);
			imswitcher.setInAnimation(rdin);
			Rotate3D rdout = new Rotate3D(-15, -90, 0, halfWidth, halfHeight);

			rdout.setDuration(duration);
			rdout.setFillAfter(true);
			imswitcher.setOutAnimation(rdout);

			i = (i + 1);
			int p = i % 4;

			if (p >= 0) {
				setpic(p);
				imswitcher.setImageResource(imageIds[p]);
			} else {

				int k = 4 + p;
				setpic(k);
				imswitcher.setImageResource(imageIds[k]);
			}

		}

	}
}