滑動按鈕
1. 創(chuàng)建一個類SlideButton繼承View
2.實現(xiàn)帶有兩個參數(shù)的構造器(獲取兩個位圖)
?public SlideButton(Context context,AttributeSet attrs) {
???? super(context, attrs);
???? initPaint();
???? initBmp();
?}
?private void initPaint() {
???? mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
?}
?private void initBmp() {
???? mBackgroundBmp =BitmapFactory.decodeResource(getResources(),
???????????? R.drawable.switch_background);
???? mSlideBmp =BitmapFactory.decodeResource(getResources(),
???? ????????R.drawable.slide_button_background);
?}
3. 實現(xiàn)onMeasure()設置控件大小為底部位圖的大小
?@Override
?protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
????setMeasuredDimension(mBackgroundBmp.getWidth(),
???????????? mBackgroundBmp.getHeight());
?}
4. 實現(xiàn)onDraw() 繪制默認的圖片
?@Override
?protected void onDraw(Canvas canvas) {
???? canvas.drawBitmap(mBackgroundBmp, 0, 0,mPaint);
???? canvas.drawBitmap(mSlideBmp, 0, 0,mPaint);
?}
5. 實現(xiàn)onTouchEvent() 滑動的時候控制滑塊的位置 注意設置滑塊的范圍
?private float mSlideLeft=0;
?@Override
?protected void onDraw(Canvas canvas) {
???? canvas.drawBitmap(mBackgroundBmp, 0, 0,mPaint);
???? if (mSlideLeft<0) {
???????? canvas.drawBitmap(mSlideBmp, 0, 0,mPaint);
???? }else if(mSlideLeft>mBackgroundBmp.getWidth()-mSlideBmp.getWidth()) {
???????? canvas.drawBitmap(mSlideBmp,mBackgroundBmp.getWidth()-
mSlideBmp.getWidth(),0, mPaint);
???? }else {
???????? canvas.drawBitmap(mSlideBmp,mSlideLeft, 0, mPaint);
???? }
?}
?@Override
?public boolean onTouchEvent(MotionEvent event){
???? switch (event.getAction()) {
???? ????case MotionEvent.ACTION_DOWN:
???????? ????break;
???? ????case MotionEvent.ACTION_MOVE:
???????? ????float touchX=event.getX();
????????????mSlideLeft=touchX-mSlideBmp.getWidth()/2;
???????? ????break;
???? ????case MotionEvent.ACTION_UP:
???????? ????floatslideHalf=mSlideLeft+mSlideBmp.getWidth()/2;
???????? ????floatbackgroundHalf=mBackgroundBmp.getWidth()/2;
???????? ????if (slideHalf>backgroundHalf) {
????????????????mSlideLeft=mBackgroundBmp.getWidth()-mSlideBmp.getWidth();
???????? ????}else {
?????? ??????????mSlideLeft=0;
???????? ????}
???????? ????break;
???? }
???? //刷新界?面 讓系統(tǒng)調?用onDraw()
???? invalidate();
???? return true;
?}
6. 在onDraw()中根據(jù)最后的界面決定當前控件的狀態(tài)
?@Override
?protected void onDraw(Canvas canvas) {
???? ...
???? //根據(jù)當前的狀態(tài)決定開關
???? if (mSlideLeft==0&&mIsOpen) {
???????? mIsOpen=false;
???????? ..
???? }elseif(mSlideLeft==mBackgroundBmp.getWidth()-
mSlideBmp.getWidth()&&!mIsOpen){
???????? mIsOpen=true;
???????? ..
???? }
?}
7. 創(chuàng)建開關的回調接口
public interface ISlideButtonChangeListener{
???? public void onButtonStateChanged(booleanflag);
?}
?public class SlideButton extends View {
???? private ISlideButtonChangeListenermListener;
???? public voidsetmListener(ISlideButtonChangeListener mListener) {
???????? this.mListener = mListener;
???? }
???? @Override
???? protected void onDraw(Canvas canvas) {
???????? ...
???????? //根據(jù)當前的狀態(tài)決定開關
???????? if (mSlideLeft==0&&mIsOpen) {
???????????? mIsOpen=false;
???????????? if (mListener!=null) {
????????????????mListener.onButtonStateChanged(mIsOpen);
???????????? }
???????? }elseif(mSlideLeft==mBackgroundBmp.getWidth()-
mSlideBmp.getWidth()&&!mIsOpen){
???????????? mIsOpen=true;
???????????? if (mListener!=null) {
????????????????mListener.onButtonStateChanged(mIsOpen);
???????????? }
???????? }
???? }
?}