Android上实现IOS的switch button
2012-11-15 15:54:11 来源:WEB开发网核心提示:import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Matri
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class SlipButton extends View implements OnTouchListener { private boolean mNowChecked = false; private boolean mOnSlip = false; private float mDownX, mNowX; private Rect mBtnOn, mBtnOff; private boolean isChgLsnOn = false; private OnChangedListener mChgLsn; private Bitmap mOnBg, mOffBg, mSlipBg; public SlipButton(Context context) { super(context); init(); } public SlipButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { mOnBg = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_on); mOffBg = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_off); mSlipBg = BitmapFactory.decodeResource(getResources(), R.drawable.slip_btn); mBtnOn = new Rect(0, 0, mSlipBg.getWidth(), mSlipBg.getHeight()); mBtnOff = new Rect(mOffBg.getWidth() - mSlipBg.getWidth(), 0, mOffBg.getWidth(), mSlipBg.getHeight()); setOnTouchListener(this); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Matrix matrix = new Matrix(); Paint paint = new Paint(); float x; { if (!mNowChecked) canvas.drawBitmap(mOffBg, matrix, paint); else canvas.drawBitmap(mOnBg, matrix, paint); if (mOnSlip) { if (mNowX >= mOnBg.getWidth()) x = mOnBg.getWidth() - mSlipBg.getWidth() / 2; else x = mNowX - mSlipBg.getWidth() / 2; } else { if (mNowChecked) x = mBtnOff.left; else x = mBtnOn.left; } if (x < 0) x = 0; else if (x > mOnBg.getWidth() - mSlipBg.getWidth()) x = mOnBg.getWidth() - mSlipBg.getWidth(); canvas.drawBitmap(mSlipBg, x, 0, paint); } } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: mNowX = event.getX(); break; case MotionEvent.ACTION_DOWN: if (event.getX() > mOnBg.getWidth() || event.getY() > mOnBg.getHeight()) return false; mDownX = event.getX(); mNowX = mDownX; break; case MotionEvent.ACTION_UP: mOnSlip = false; boolean LastChoose = mNowChecked; if (!mNowChecked) mNowChecked = true; else mNowChecked = false; if (isChgLsnOn && (LastChoose != mNowChecked)) mChgLsn.OnChanged(mNowChecked); break; default: } invalidate(); return true; } public void setChecked(boolean checked) { if (mNowChecked != checked) { mNowChecked = checked; invalidate(); } } public void setOnChangedListener(OnChangedListener l) { isChgLsnOn = true; mChgLsn = l; } public interface OnChangedListener { abstract void OnChanged(boolean CheckState); } }
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››iOS 前台和后台交互
- ››Android为ListView的Item设置不同的布局
更多精彩
赞助商链接