package com.yang.applock;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class MyToggleButton extends View implements OnClickListener{

	/**
	 * 閸嬫矮璐熼懗灞炬珯閻ㄥ嫬娴橀悧锟?
	 */
	private Bitmap backgroundBitmap;
	/**
	 * 閸欘垯浜掑鎴濆З閻ㄥ嫬娴橀悧锟?
	 */
	private Bitmap slideBtn;
	private Paint paint;
	/**
	 * 濠婃垵濮╅幐澶愭尦閻ㄥ嫬涔忔潏鐟扮溇
	 */
	private float slideBtn_left;

	/**
	 * 閸︺劋鍞惍渚€鍣烽棃銏犲灡瀵ゅ搫顕挒锛勬畱閺冭泛锟芥瑱绱濇担璺ㄦ暏濮濄倖鐎柅鐘虫煙濞夛拷
	 */
	public MyToggleButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 閸︺劌绔风仦锟介弬鍥︽娑擃厼锛愰崥宥囨畱view閿涘苯鍨卞鐑樻閻㈣京閮寸紒鐔诲殰閸斻劏鐨熼悽銊o拷锟?
	 * @param context	娑撳﹣绗呴弬鍥ь嚠鐠烇拷
	 * @param attrs		鐏炵偞锟窖囨肠
	 */
	public MyToggleButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		initView();
	}
	
	/**
	 * 閸掓繂顫愰崠锟?
	 */
	private void initView() {
		
		//閸掓繂顫愰崠鏍ф禈閻楋拷
		backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.switch_background);
		slideBtn = BitmapFactory.decodeResource(getResources(), R.drawable.slide_button);
		
		
		//閸掓繂顫愰崠锟?閻㈣崵鐟?
		paint = new Paint();
		paint.setAntiAlias(true); // 閹垫挸绱戦幎妤冪叐姒伙拷
		
		
		//濞h濮瀘nclick娴滃娆㈤惄鎴濇儔
		setOnClickListener(this);
	}

	/*
	 * view 鐎电钖勯弰鍓с仛閻ㄥ嫬鐫嗛獮鏇氱瑐閿涘本婀侀崙鐘遍嚋闁插秷顩﹀銉╊€冮敍锟?
	 * 1閵嗕焦鐎柅鐘虫煙濞夛拷 閸掓稑缂?鐎电钖勯妴锟?
	 * 2閵嗕焦绁撮柌寮檌ew閻ㄥ嫬銇囩亸蹇嬶拷锟?onMeasure(int,int);
	 * 3閵嗕胶鈥樼€规iew閻ㄥ嫪缍呯純锟?閿涘瘉iew閼奉亣闊╅張澶夌娴滄稑缂撶拋顔芥綀閿涘苯鍠呯€规碍娼堥崷锟?閻栫iew閹靛鑵戦妴锟? onLayout();
	 * 4閵嗕胶绮崚锟?view 閻ㄥ嫬鍞寸€癸拷 閵嗭拷 onDraw(Canvas)
	 */
	
	@Override
	/**
	 * 濞村鍣虹亸鍝勵嚟閺冨墎娈戦崶鐐剁殶閺傝纭?
	 */
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		/**
		 * 鐠佸墽鐤嗚ぐ鎾冲view閻ㄥ嫬銇囩亸锟?
		 * width  :view閻ㄥ嫬顔旀惔锟?
		 * height :view閻ㄥ嫰鐝惔锟?  閿涘牆宕熸担宥忕窗閸嶅繒绀岄敍锟?
		 */
		setMeasuredDimension(backgroundBitmap.getWidth(),backgroundBitmap.getHeight());
	}

	//绾喖鐣炬担宥囩枂閻ㄥ嫭妞傞崐娆掔殶閻劍顒濋弬瑙勭《
	//閼奉亜鐣炬稊濉縤ew閻ㄥ嫭妞傞崐娆欑礉娴f粎鏁ゆ稉宥呫亣
//	@Override
//	protected void onLayout(boolean changed, int left, int top, int right,
//			int bottom) {
//		super.onLayout(changed, left, top, right, bottom);
//	}
	
	/**
	 * 瑜版挸澧犲锟介崗宕囨畱閻樿埖锟斤拷
	 *  true 娑撳搫绱?
	 */
	private boolean currState = false;
	
	@Override
	/**
	 * 缂佹ê鍩楄ぐ鎾冲view閻ㄥ嫬鍞寸€癸拷
	 */
	protected void onDraw(Canvas canvas) {
//		super.onDraw(canvas);
		
		// 缂佹ê鍩?閼冲本娅?
		/*
		 * backgroundBitmap	鐟曚胶绮崚鍓佹畱閸ュ墽澧?
		 * left	閸ュ墽澧栭惃鍕箯鏉堢懓鐪?
		 * top	閸ュ墽澧栭惃鍕瑐鏉堢懓鐪?
		 * paint 缂佹ê鍩楅崶鍓у鐟曚椒濞囬悽銊ф畱閻㈣崵鐟?
		 */
		canvas.drawBitmap(backgroundBitmap, 0, 0, paint);
		
		//缂佹ê鍩?閸欘垱绮﹂崝銊ф畱閹稿鎸?
		canvas.drawBitmap(slideBtn, slideBtn_left, 0, paint);
	}

	/**
	 * 閸掋倖鏌囬弰顖氭儊閸欐垹鏁撻幏鏍уЗ閿涳拷
	 * 婵″倹鐏夐幏鏍уЗ娴滃棴绱濈亸鍙樼瑝閸愬秴鎼锋惔锟?onclick 娴滃娆?
	 * 
	 */
	private boolean isDrag = false;
	@Override
	/**
	 * onclick 娴滃娆㈤崷鈺抜ew.onTouchEvent 娑擃叀顫︾憴锝嗙€介妴锟?
	 * 缁崵绮虹€电nclick 娴滃娆㈤惃鍕掗弸鎰剁礉鏉╁洣绨粻锟介梽瀣剁礉閸欘亣顩﹂張濉猳wn 娴滃娆? up 娴滃娆㈤敍宀€閮寸紒鐔峰祮鐠併倓璐?閸欐垹鏁撴禍鍝籰ick 娴滃娆?
	 * 
	 */
	public void onClick(View v) {
		/*
		 * 婵″倹鐏夊▽鈩冩箒閹锋牕濮╅敍灞惧閹笛嗩攽閺€鐟板綁閻樿埖锟戒胶娈戦崝銊ょ稊
		 */
		if(!isDrag){
			currState = !currState;
			flushState();
		}
	}


	
	/**
	 * down 娴滃娆㈤弮鍓佹畱x閸婏拷
	 */
	private int firstX;
	/**
	 * touch 娴滃娆㈤惃鍕瑐娑擄拷娑撶崅閸婏拷
	 */
	private int lastX;
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			firstX = lastX =(int) event.getX();
			isDrag = false;
			
			break;
		case MotionEvent.ACTION_MOVE:
			
			//閸掋倖鏌囬弰顖氭儊閸欐垹鏁撻幏鏍уЗ
			if(Math.abs(event.getX()-firstX)>5){
				isDrag = true;
			}
			
			//鐠侊紕鐣?閹靛瀵氶崷銊ョ潌楠炴洑绗傜粔璇插З閻ㄥ嫯绐涚粋锟?
			int dis = (int) (event.getX() - lastX);
			
			//鐏忓棙婀板▎锛勬畱娴e秶鐤?鐠佸墽鐤嗙紒妾抋stX
			lastX = (int) event.getX();
			
			//閺嶈宓侀幍瀣瘹缁夎濮╅惃鍕獩缁備紮绱濋弨鐟板綁slideBtn_left 閻ㄥ嫬锟斤拷
			slideBtn_left = slideBtn_left+dis;
			break;
		case MotionEvent.ACTION_UP:
			
			//閸︺劌褰傞悽鐔稿珛閸斻劎娈戦幆鍛枌娑撳绱濋弽瑙勫祦閺堬拷閸氬海娈戞担宥囩枂閿涘苯鍨介弬顓炵秼閸撳秴绱戦崗宕囨畱閻樿埖锟斤拷
			if (isDrag) {

				int maxLeft = backgroundBitmap.getWidth() - slideBtn.getWidth(); // slideBtn
																					// 瀹革箒绔熺仦濠冩付婢堆冿拷锟?
				/*
				 * 閺嶈宓?slideBtn_left 閸掋倖鏌囬敍灞界秼閸撳秴绨查弰顖欑矆娑斿牏濮搁幀锟?
				 */
				if (slideBtn_left > maxLeft / 2) { // 濮濄倖妞傛惔鏂捐礋 閹垫挸绱戦惃鍕Ц閹拷
					currState = true;
				} else {
					currState = false;
				}

				flushState();
			}
			break;
		}
		
		flushView();
		
		return true; 
	}

	/**
	 * 閸掗攱鏌婅ぐ鎾冲閻樿埖锟斤拷
	 */
	private void flushState() {
		if(currState){
			slideBtn_left = backgroundBitmap.getWidth()-slideBtn.getWidth();
		}else{
			slideBtn_left = 0;
		}
		
		flushView(); 
	}
	
	/**
	 * 閸掗攱鏌婅ぐ鎾冲鐟欏棗濮?
	 */
	private void flushView() {
		/*
		 * 鐎碉拷 slideBtn_left  閻ㄥ嫬锟借壈绻樼悰灞藉灲閺傦拷 閿涘瞼鈥樻穱婵嗗従閸︺劌鎮庨悶鍡欐畱娴e秶鐤?閸楋拷       0<=slideBtn_left <=  maxLeft
		 * 
		 */
		
		int maxLeft = backgroundBitmap.getWidth()-slideBtn.getWidth();	//	slideBtn 瀹革箒绔熺仦濠冩付婢堆冿拷锟?
		
		//绾喕绻?slideBtn_left >= 0
		slideBtn_left = (slideBtn_left>0)?slideBtn_left:0;
		
		//绾喕绻?slideBtn_left <=maxLeft
		slideBtn_left = (slideBtn_left<maxLeft)?slideBtn_left:maxLeft;
		
		/*
		 * 閸掗攱鏌婅ぐ鎾冲鐟欏棗娴? 鐎佃壈鍤?閹笛嗩攽onDraw閹笛嗩攽
		 */
		invalidate();
	}

}