android SlipButton
SlipButton.java:
package com.wistron.other; 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 { public interface OnChangedListener { abstract void OnChanged(String strName, boolean CheckState); } private String strName; private boolean enabled = true; public boolean flag = false; public boolean NowChoose = false; private boolean OnSlip = false; public float DownX = 0f, NowX = 0f; private Rect Btn_On, Btn_Off; private boolean isChgLsnOn = false; private OnChangedListener ChgLsn; private Bitmap bg_on, bg_off, slip_btn; public SlipButton(Context context) { super(context); init(); } public SlipButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public void setChecked(boolean fl) { if (fl) { flag = true; NowChoose = true; NowX = 80; } else { flag = false; NowChoose = false; NowX = 0; } } public void setEnabled(boolean b) { if (b) { enabled = true; } else { enabled = false; } } private void init() { bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.on); bg_off = BitmapFactory.decodeResource(getResources(), R.drawable.off); slip_btn = BitmapFactory.decodeResource(getResources(), R.drawable.white); Btn_On = new Rect(0, 0, slip_btn.getWidth(), slip_btn.getHeight()); Btn_Off = new Rect(bg_off.getWidth() - slip_btn.getWidth(), 0, bg_off.getWidth(), slip_btn.getHeight()); setOnTouchListener(this); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Matrix matrix = new Matrix(); Paint paint = new Paint(); float x; { if (flag) { NowX = 80; flag = false; } if (NowX < (bg_on.getWidth() / 2)) canvas.drawBitmap(bg_off, matrix, paint); else canvas.drawBitmap(bg_on, matrix, paint); if (OnSlip) { if (NowX >= bg_on.getWidth()) x = bg_on.getWidth() - slip_btn.getWidth() / 2; else x = NowX - slip_btn.getWidth() / 2; } else { if (NowChoose) x = Btn_Off.left; else x = Btn_On.left; } if (x < 0) x = 0; else if (x > bg_on.getWidth() - slip_btn.getWidth()) x = bg_on.getWidth() - slip_btn.getWidth(); canvas.drawBitmap(slip_btn, x, 0, paint); } } public boolean onTouch(View v, MotionEvent event) { if (!enabled) { return false; } switch (event.getAction()) { case MotionEvent.ACTION_MOVE: NowX = event.getX(); break; case MotionEvent.ACTION_DOWN: if (event.getX() > bg_on.getWidth() || event.getY() > bg_on.getHeight()) return false; OnSlip = true; DownX = event.getX(); NowX = DownX; break; case MotionEvent.ACTION_UP: OnSlip = false; boolean LastChoose = NowChoose; if (event.getX() >= (bg_on.getWidth() / 2)) NowChoose = true; else NowChoose = false; if (isChgLsnOn && (LastChoose != NowChoose)) ChgLsn.OnChanged(strName, NowChoose); break; default: } invalidate(); return true; } public void SetOnChangedListener(String name, OnChangedListener l) { strName = name; isChgLsnOn = true; ChgLsn = l; } }
Activity中应用:
slipButton = (SlipButton)findViewById(R.id.slipbutton); slipButton.SetOnChangedListener("WIFI", this); slipButton.setChecked(true); @Override public void OnChanged(String strName, boolean CheckState) { // TODO Auto-generated method stub if(CheckState){ Toast.makeText(this, strName + "is opened!", 1000).show(); mHandler.sendEmptyMessage(1); }else{ Toast.makeText(this, strName + "is closed!", 1000).show(); mHandler.sendEmptyMessage(2); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28