SlideSwitch
代码很简单废话就不多说了 直接上代码。
package com.osrv.ecc.easycontrolcar.Util.View;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.osrv.ecc.easycontrolcar.R;
/**
* Created by yj on 2018/4/13.
*/
public class SlideSwitch extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint aPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
boolean isOn = false;
float curX = 0;
float centerY;
float viewWidth;
float radius;
float lineStart;
float lineEnd;
float lineWidth;
final int SCALE = 4;
public boolean isShowAnim=false;
private int bigPoint=0;
OnStateChangedListener onStateChangedListener;
private ValueAnimator animator;
public SlideSwitch(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SlideSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SlideSwitch(Context context) {
super(context);
}
public void setOpenProgress(boolean openProgress) {
isShowAnim = openProgress;
if (openProgress) {
if (animator==null) {
animator = ValueAnimator.ofInt(0,3);
animator.setDuration(800);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
bigPoint = ((int) animation.getAnimatedValue());
postInvalidate();
}
});
}
animator.start();
}else {
if (animator!=null&&animator.isRunning()) {
animator.end();
}
}
}
public void setCheck(boolean ischeck){
if (ischeck){
curX=lineEnd;
isOn = true;
}else {
curX=lineStart;
isOn = false;
}
postInvalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
getParent().requestDisallowInterceptTouchEvent(true);
if (!isShowAnim) {
curX = event.getX();
if(event.getAction() == MotionEvent.ACTION_UP)
{
if(curX > viewWidth / 2)
{
curX = lineEnd;
if(false == isOn)
{
if(null != onStateChangedListener)
{
onStateChangedListener.onStateChanged(true);
}
isOn = true;
}
}
else
{
curX = lineStart;
if(true == isOn)
{
if(null != onStateChangedListener)
{
onStateChangedListener.onStateChanged(false);
}
isOn = false;
}
}
}
if (curX<lineStart){
curX=lineStart;
}
if (curX>lineEnd){
curX=lineEnd;
}
this.postInvalidate();
}
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.setMeasuredDimension(this.getMeasuredWidth(), this.getMeasuredWidth() * 2 / SCALE);
viewWidth = this.getMeasuredWidth();
radius = viewWidth / SCALE;
lineWidth = radius * 2f;
curX = radius;
centerY = this.getMeasuredWidth() / SCALE;
lineStart = radius;
lineEnd = (SCALE - 1) * radius;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
curX = curX > lineEnd?lineEnd:curX;
curX = curX < lineStart?lineStart:curX;
if (isShowAnim){
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(lineWidth);
mPaint.setColor(getResources().getColor(R.color.color999999));
canvas.drawLine(lineStart, centerY, lineEnd, centerY, mPaint);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(lineEnd, centerY, radius, mPaint);
canvas.drawCircle(lineStart, centerY, radius, mPaint);
drawAmin(canvas);
}else {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(lineWidth);
mPaint.setColor(getResources().getColor(R.color.colorPrimary));
canvas.drawLine(lineStart, centerY, curX, centerY, mPaint);
mPaint.setColor(getResources().getColor(R.color.color999999));
canvas.drawLine(curX, centerY, lineEnd, centerY, mPaint);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(getResources().getColor(R.color.color999999));
canvas.drawCircle(lineEnd, centerY, lineWidth / 2, mPaint);
mPaint.setColor(getResources().getColor(R.color.colorPrimary));
canvas.drawCircle(lineStart, centerY, lineWidth / 2, mPaint);
mPaint.setColor(getResources().getColor(R.color.AmostBGColor));
canvas.drawCircle(curX, centerY, radius, mPaint);
}
}
private void drawAmin(Canvas canvas) {
float v = lineEnd - lineStart;
switch (bigPoint){
case 0:{
mPaint.setColor(getResources().getColor(R.color.WhiteBg));
canvas.drawRect( new RectF(lineStart+v/2-lineWidth/10,lineWidth*2/5,lineStart+v/2+lineWidth/10,lineWidth*3/5),mPaint);
mPaint.setColor(getResources().getColor(R.color.colord2d2d2));
canvas.drawRect( new RectF(lineStart+v/2-lineWidth/2,lineWidth*2/5,lineStart+v/2-lineWidth*3/10,lineWidth*3/5),mPaint);
mPaint.setColor(getResources().getColor(R.color.colorb5b5b5));
canvas.drawRect( new RectF(lineStart+v/2+lineWidth*3/10,lineWidth*2/5,lineStart+v/2+lineWidth/2,lineWidth*3/5),mPaint);
}
break;
case 1:{
mPaint.setColor(getResources().getColor(R.color.colorb5b5b5));
canvas.drawRect( new RectF(lineStart+v/2-lineWidth/10,lineWidth*2/5,lineStart+v/2+lineWidth/10,lineWidth*3/5),mPaint);
mPaint.setColor(getResources().getColor(R.color.WhiteBg));
canvas.drawRect( new RectF(lineStart+v/2-lineWidth/2,lineWidth*2/5,lineStart+v/2-lineWidth*3/10,lineWidth*3/5),mPaint);
mPaint.setColor(getResources().getColor(R.color.colord2d2d2));
canvas.drawRect( new RectF(lineStart+v/2+lineWidth*3/10,lineWidth*2/5,lineStart+v/2+lineWidth/2,lineWidth*3/5),mPaint);
}
break;
case 2:{
mPaint.setColor(getResources().getColor(R.color.colord2d2d2));
canvas.drawRect( new RectF(lineStart+v/2-lineWidth/10,lineWidth*2/5,lineStart+v/2+lineWidth/10,lineWidth*3/5),mPaint);
mPaint.setColor(getResources().getColor(R.color.colorb5b5b5));
canvas.drawRect( new RectF(lineStart+v/2-lineWidth/2,lineWidth*2/5,lineStart+v/2-lineWidth*3/10,lineWidth*3/5),mPaint);
mPaint.setColor(getResources().getColor(R.color.WhiteBg));
canvas.drawRect( new RectF(lineStart+v/2+lineWidth*3/10,lineWidth*2/5,lineStart+v/2+lineWidth/2,lineWidth*3/5),mPaint);
}
break;
}
bigPoint++;
}
public void setOnStateChangedListener(OnStateChangedListener o)
{
this.onStateChangedListener = o;
}
public interface OnStateChangedListener
{
public void onStateChanged(boolean state);
}
}