安卓自带的switch可自定义性太差。而且在不同手机上显示效果可能不同。我就遇到再4.4上是长方形方块,在5.0上是圆弧形。
而且经测试switch再华为手机无法监听点击事件,应该是华为手机bug。未深入研究,深受安卓自带switch苦的我自定义View绘制了一个switch.
下面放效果图
点击带有滚动效果。需要的同学可以拿去。
下面放上源码。转载请注明出处:
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; import com.airainbow.secret_expert.R; import com.airainbow.secret_expert.timer.BaseTimer; /** * 编写 :易魏。 * 日期:20180414 * 功能 自定义switch * Created by Administrator on 2018/4/14. */ public class MySwitch extends View { private int checkColor;//背景选中颜色 private int noCheckColor;//背景未选中颜色 private int circleColor;//圆选中的颜色 private boolean isCheck;//是否勾选 private float outLineWidth;//外围描边宽度 private int outLineColor;//外围描边颜色 private int circleColorFalse;//圆未选中颜色 private int checkColorDefault = 0xffff4081;//粉色 ARGB private int circleColorDefault = 0xffffffff;//白色 private int circleColorFalseDefault=0xffff4081; private int noCheckColorDefault = 0xffffffff;//白色 private float outLineWidthDefault = 2; private int outLineColorDefault = 0x74747400; private boolean isCheckDefault = false; private Path outPath = new Path(); private Paint outPaint = new Paint(); private Path inPath = new Path(); private Paint inPaint = new Paint(); private Paint circlePaint = new Paint(); private int circleCenter = 0; private int width, height; private ChangeSwitchStatus changeSwitchStatus = new ChangeSwitchStatus(); private SwitchOnClickListener switchOnClickListener; public MySwitch(Context context) { super(context); } public MySwitch(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MySwitch)