package com.view;

import javax.microedition.khronos.egl.EGLContext;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.widget.ImageView;

public class AnimPage extends Activity implements OnGestureListener{
	private static final int X_MAX=800;
	private static final int Y_MAX=600;
	private int scrollX=260;
	private int scrollY=130;

	MyView main;
	Bitmap bmp;
	Bitmap adapt;
	Resources res;
	Paint paint;
	GestureDetector gestureScanner;
	
	ImageView iv_pnt;
	ImageView iv_slide;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		gestureScanner=new GestureDetector(this);
		paint=new Paint();
		res=getResources();
		bmp=BitmapFactory.decodeResource(res,R.drawable.slide_pnt);
		//adapt=Bitmap.createBitmap(bmp);
		adapt=Bitmap.createScaledBitmap(bmp, 30, 30, true);
		main=new MyView(this);
		

		this.setContentView(R.layout.animpageeg);
		
		iv_slide=(ImageView)findViewById(R.id.touch_slide_in_anim);
		//iv_slide.
		iv_pnt=new ImageView(this);
		iv_pnt.setImageBitmap(adapt);
		int l=iv_slide.getLeft();int t=iv_slide.getTop();
		iv_pnt.layout(l+10, t, l+40, t+30);
		//iv_pnt.setLayoutParams(new ViewGroup.LayoutParams(30,30));
		
		//main.layout(l+10, t, l+40, 4+30);
		
		this.addContentView(iv_pnt, new ViewGroup.LayoutParams(30,30));
		

	}



		public boolean onTouchEvent(MotionEvent me)
		{
			//this.handleIvScroll(iv_slide.getLeft()+10,(int) me.getY());
			//int l=iv_slide.getLeft();int t=iv_slide.getTop();
			//iv_pnt.invalidate(l+10, (int)me.getY(), l+40, (int)me.getY()+30);
			return gestureScanner.onTouchEvent(me);
		}

		public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)
		{
			//main.handleScroll(distanceX,distanceY);
			
			//iv_pnt.invalidate(l+10, t+(int)distanceY, l+40, t+(int)distanceY+30);
			//iv_pnt.setLayoutParams(new ViewGroup.LayoutParams(iv_slide.getLayoutParams().))
			//int l=iv_slide.getLeft();int t=iv_slide.getTop();
			//this.handleIvScroll(l+10, t-(int)distanceY);
			return true;
		}

		public boolean onDown(MotionEvent e){return true;}

		public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)
		{	int l=iv_slide.getLeft();int t=iv_slide.getTop();
		this.handleIvScroll(l+10, t-(int)(e2.getY()-e1.getY()));
			return true;}

		public void onLongPress(MotionEvent e){
			//this.handleIvScroll(iv_slide.getLeft()+10,(int) e.getY());
		}

		public void onShowPress(MotionEvent e){
			//this.handleIvScroll(iv_slide.getLeft()+10,(int) e.getY());
		}

		public boolean onSingleTapUp(MotionEvent e)
		{	return true;}
		


	class MyView extends View {
		public MyView(Context context)
		{super(context);}

		protected void onDraw(Canvas canvas)
		{
			//canvas.drawBitmap(adapt, new Matrix(), paint);
		}

		public void handleScroll(float distX,float distY)
		{
			/*
			if(distX>6.0){
				if(scrollX<460)
				{scrollX+=15;}
			}else if(distX<-6.0){
				if(scrollX>=15)
				{scrollX-=15;}
			}*/
		
			if (distY > 6.0) {
				if (scrollY < 410) {
					scrollY += 15;
				}
			} else if (distY < -6.0) {
				if (scrollY >= 140) {
					scrollY -= 15;
				}
			}
   // //////////////////////////////////////////
   //
   // if ((scrollX <= 480) && (scrollY <= 120)) {
   // adapt = Bitmap.createBitmap(bmp, scrollX, scrollY, 320, 480);
   // invalidate();
   // }
			
			invalidate();
		}
	}
	
	public void handleIvScroll(int x,int y)
	{
		iv_pnt.layout(x, y, x+30, y+30);
		iv_pnt.invalidate();
		
	}
}
