import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class SwitchView extends View implements View.OnTouchListener { private int downx, downy; private boolean isfling = false; private float width = 0; private float height = 0; private float radius = 0; private boolean state = false; private boolean initstate = false; private onChangeListener listener; private boolean touch = false; public SwitchView(Context context) { super(context); } public SwitchView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public SwitchView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); setOnTouchListener(this); if (downx < 0 ){ state = false; checkedBg(canvas, "#cfcfcf","#c1c1c1"); slipperBtn(canvas, radius, radius, radius - 4); }else if(downx > width){ state = true; checkedBg(canvas, "#00aaef","#0098d5"); //checked slipperBtn(canvas, width - radius, radius, radius - 4); }else{ if (downx > width / 2) { if (isfling) { state = true; checkedBg(canvas, "#00aaef","#0098d5"); //checked slipperBtn(canvas, downx, radius, radius - 4); } else { state = true; checkedBg(canvas, "#00aaef","#0098d5"); //checked slipperBtn(canvas, width - radius, radius, radius - 4); } } else { if (isfling) { state = false; checkedBg(canvas, "#cfcfcf","#c1c1c1"); //unchecked slipperBtn(canvas, downx, radius, radius - 4); } else { state = false; checkedBg(canvas, "#cfcfcf","#c1c1c1"); slipperBtn(canvas, radius, radius, radius - 4); } } } if (listener != null) { listener.OnChanged(this, state,touch); } } public void setWidth(int width) { this.width = width * 3 / 17f; this.height = this.width * 2 / 5f; this.radius = this.height / 2f; } public void checkedBg(Canvas canvas, String color, String outsidecolor) { Paint p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.FILL); p.setColor(Color.parseColor(color)); RectF rec = new RectF(0, 0, width, height); canvas.drawRoundRect(rec, radius, radius, p); Paint po = new Paint(); po.setAntiAlias(true); po.setStyle(Paint.Style.STROKE); po.setColor(Color.parseColor(outsidecolor)); RectF reco = new RectF(0, 0, width, height); canvas.drawRoundRect(reco, radius, radius, po); } public void slipperBtn(Canvas canvas, float cx, float cy, float radius) { Paint p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.FILL); p.setColor(Color.parseColor("#ffffff")); canvas.drawCircle(cx, cy, radius, p); } @Override public boolean onTouch(View v, MotionEvent event) { downx = (int) event.getX(); downy = (int) event.getY(); touch = true; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isfling = true; break; case MotionEvent.ACTION_MOVE: isfling = true; break; case MotionEvent.ACTION_UP: isfling = false; break; } invalidate(); return true; } public void setOnChanged(onChangeListener changelistener) { this.listener = changelistener; } public interface onChangeListener { public void OnChanged(SwitchView switchView, boolean checkState,boolean touched); } public void setInitChanges(boolean changes){ if(changes){ downx = (int)width; }else{ downx = 0; } isfling = false; invalidate(); } }
用Canvas画Switch控件
最新推荐文章于 2024-03-30 04:30:00 发布