自定义view — Toggle开关

mainActivity

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ToggleView tv_test = (ToggleView) findViewById(R.id.tv_test);
		boolean toggleOn = tv_test.isToggleOn();
		//处理事件
		tv_test.setOnToggleChangeListener(new OnToggleChangeListener() {
			
			@Override
			public void onToggleChanged(ToggleView toggleView, boolean isToggleOn) {
				// TODO Auto-generated method stub
				//自己处理
				if (isToggleOn) {
					Toast.makeText(getApplicationContext(), "开关打开了", 0).show();
				} else {
					Toast.makeText(getApplicationContext(), "开关关闭了", 0).show();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


Toogleview

public class ToggleView extends View {

	private Bitmap mBitmapBackgroud;
	private Bitmap mBitmapButton;
	private Paint mPaint;
	private float downOrMoveOrUpX;

	private static final int STATE_DOWN = 1; // 按下
	private static final int STATE_MOVE = 2; // 移动
	private static final int STATE_UP = 3; // 松开

	private int current_State = STATE_DOWN;// 0 记录事件的状态
	
	private boolean isToggleOn = false;//记录开关的状态
	
	private OnToggleChangeListener mOnToggleChangeListener;
	//事件回调声明
	public interface OnToggleChangeListener {
		public void onToggleChanged(ToggleView toggleView, boolean isToggleOn);
	}
	
	public void setOnToggleChangeListener(OnToggleChangeListener listener) {
		this.mOnToggleChangeListener = listener;
	}
	
	/**
	 * @return
	 *    当前开关的状态
	 */
	public boolean isToggleOn(){
		return isToggleOn;
	}

	public ToggleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initView();

		mPaint = new Paint();
	}

	private void initView() {
		// 获取两张图片
		// 按钮的背景图片
		mBitmapBackgroud = BitmapFactory.decodeResource(getResources(),
				R.drawable.switch_background);

		// 滑块
		mBitmapButton = BitmapFactory.decodeResource(getResources(),
				R.drawable.slide_button_background);

	}

	public ToggleView(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// 控件的大小 为 背景图片的大小
		setMeasuredDimension(mBitmapBackgroud.getWidth(),
				mBitmapBackgroud.getHeight());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// 添加触摸事件
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// 按下
			downOrMoveOrUpX = event.getX();
			current_State = STATE_DOWN;
		case MotionEvent.ACTION_MOVE:
			// 移动
			downOrMoveOrUpX = event.getX();
			current_State = STATE_MOVE;
			break;
		case MotionEvent.ACTION_UP:
			//松开
			downOrMoveOrUpX = event.getX();    
			current_State = STATE_UP;
			//判断
			if (downOrMoveOrUpX < mBitmapBackgroud.getWidth()/2 && isToggleOn) {
				//左一半
				isToggleOn = false;
				//调用回调接口
				if (mOnToggleChangeListener != null) {
					mOnToggleChangeListener.onToggleChanged(this, isToggleOn);
				}
			} else if (downOrMoveOrUpX >= mBitmapBackgroud.getWidth()/2 && !isToggleOn){
				isToggleOn = true;
				//调用回调接口
				if (mOnToggleChangeListener != null) {
					mOnToggleChangeListener.onToggleChanged(this, isToggleOn);
				}
			}
			
			
			
			break;
		default:
			break;
		}

		invalidate();// 触发onDraw方法调用
		return true;// 自己消费掉事件
	}

	@Override
	protected void onDraw(Canvas canvas) {

		// 绘制背景
		canvas.drawBitmap(mBitmapBackgroud, 0, 0, mPaint);

		// 绘制button
		if (current_State == STATE_DOWN || current_State == STATE_MOVE) {
			if (downOrMoveOrUpX < mBitmapButton.getWidth() / 2) {
				// 不用绘制 左边界
				canvas.drawBitmap(mBitmapButton, 0, 0, mPaint);
			} else if (downOrMoveOrUpX > (mBitmapBackgroud.getWidth() - mBitmapButton
					.getWidth() / 2)) {
				// 右边界
				canvas.drawBitmap(mBitmapButton, mBitmapBackgroud.getWidth()
						- mBitmapButton.getWidth(), 0, mPaint);
			} else {
				// 绘制
				float left = downOrMoveOrUpX - mBitmapButton.getWidth() / 2;
				canvas.drawBitmap(mBitmapButton, left, 0, mPaint);
			}
		} else if (current_State == STATE_UP) {
			
			//松开
			//1. 左一半
			if (isToggleOn) {
				//打开
				// 右边界
				canvas.drawBitmap(mBitmapButton, mBitmapBackgroud.getWidth()
						- mBitmapButton.getWidth(), 0, mPaint);
			} else {
				//关闭
				canvas.drawBitmap(mBitmapButton, 0, 0, mPaint);
			}
			//2. 右一半
		}

		super.onDraw(canvas);
	}

}



你可能感兴趣的:(自定义view — Toggle开关)