转载请注明出处:http://blog.csdn.net/haoaoo/article/details/52702662
SwitchButton 自定义开关,话不多说上代码。
颜色形状可自定义,效果图如下:
1.switchButton自定义代码
public class SwitchButton extends CompoundButton {
public static final float DEFAULT_BACK_MEASURE_RATIO = 1.8f;
public static final int DEFAULT_THUMB_SIZE_DP = 20;
public static final int DEFAULT_THUMB_MARGIN_DP = 2;
public static final int DEFAULT_ANIMATION_DURATION = 250;
public static final int DEFAULT_TINT_COLOR = 0x327FC2;
private static int[] CHECKED_PRESSED_STATE = new int[]{android.R.attr.state_checked, android.R.attr.state_enabled, android.R.attr.state_pressed};
private static int[] UNCHECKED_PRESSED_STATE = new int[]{-android.R.attr.state_checked, android.R.attr.state_enabled, android.R.attr.state_pressed};
private Drawable mThumbDrawable, mBackDrawable;
private ColorStateList mBackColor, mThumbColor;
private float mThumbRadius, mBackRadius;
private RectF mThumbMargin;
private float mBackMeasureRatio;
private long mAnimationDuration;
// fade back drawable or color when dragging or animating
private boolean mFadeBack;
private int mTintColor;
private PointF mThumbSizeF;
private int mCurrThumbColor, mCurrBackColor, mNextBackColor;
private Drawable mCurrentBackDrawable, mNextBackDrawable;
private RectF mThumbRectF, mBackRectF, mSafeRectF;
private Paint mPaint;
// whether using Drawable for thumb or back
private boolean mIsThumbUseDrawable, mIsBackUseDrawable;
private boolean mDrawDebugRect = false;
private ObjectAnimator mProcessAnimator;
// animation control
private float mProcess;
// temp position of thumb when dragging or animating
private RectF mPresentThumbRectF;
private float mStartX, mStartY, mLastX;
private int mTouchSlop;
private int mClickTimeout;
private Paint mRectPaint;
public SwitchButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public SwitchButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public SwitchButton(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
mClickTimeout = ViewConfiguration.getPressedStateDuration() + ViewConfiguration.getTapTimeout();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRectPaint.setStyle(Paint.Style.STROKE);
mRectPaint.setStrokeWidth(getResources().getDisplayMetrics().density);
mThumbRectF = new RectF();
mBackRectF = new RectF();
mSafeRectF = new RectF();
mThumbSizeF = new PointF();
mThumbMargin = new RectF();
mProcessAnimator = ObjectAnimator.ofFloat(this, "process", 0, 0).setDuration(DEFAULT_ANIMATION_DURATION);
mProcessAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
mPresentThumbRectF = new RectF();
Resources res = getResources();
float density = res.getDisplayMetrics().density;
Drawable thumbDrawable = null;
ColorStateList thumbColor = null;
float margin = density * DEFAULT_THUMB_MARGIN_DP;
float marginLeft = 0;
float marginRight = 0;
float marginTop = 0;
float marginBottom = 0;
float thumbWidth = density * DEFAULT_THUMB_SIZE_DP;
float thumbHeight = density * DEFAULT_THUMB_SIZE_DP;
float thumbRadius = density * DEFAULT_THUMB_SIZE_DP / 2;
float backRadius = thumbRadius;
Drawable backDrawable = null;
ColorStateList backColor = null;
float backMeasureRatio = DEFAULT_BACK_MEASURE_RATIO;
int animationDuration = DEFAULT_ANIMATION_DURATION;
boolean fadeBack = true;
int tintColor = Integer.MIN_VALUE;
TypedArray ta = attrs == null ? null : getContext().obtainStyledAttributes(attrs, R.styleable.SwitchButton);
if (ta != null) {
thumbDrawable = ta.getDrawable(R.styleable.SwitchButton_kswThumbDrawable);
thumbColor = ta.getColorStateList(R.styleable.SwitchButton_kswThumbColor);
margin = ta.getDimension(R.styleable.SwitchButton_kswThumbMargin, margin);
marginLeft = ta.getDimension(R.styleable.SwitchButton_kswThumbMarginLeft, margin);
marginRight = ta.getDimension(R.styleable.SwitchButton_kswThumbMarginRight, margin);
marginTop = ta.getDimension(R.styleable.SwitchButton_kswThumbMarginTop, margin);
marginBottom = ta.getDimension(R.styleable.SwitchButton_kswThumbMarginBottom, margin);
thumbWidth = ta.getDimension(R.styleable.SwitchButton_kswThumbWidth, thumbWidth);
thumbHeight = ta.getDimension(R.styleable.SwitchButton_kswThumbHeight, thumbHeight);
thumbRadius = ta.getDimension(R.styleable.SwitchButton_kswThumbRadius, Math.min(thumbWidth, thumbHeight) / 2.f);
backRadius = ta.getDimension(R.styleable.SwitchButton_kswBackRadius, thumbRadius + density * 2f);
backDrawable = ta.getDrawable(R.styleable.SwitchButton_kswBackDrawable);
backColor = ta.getColorStateList(R.styleable.SwitchButton_kswBackColor);
backMeasureRatio = ta.getFloat(R.styleable.SwitchButton_kswBackMeasureRatio, backMeasureRatio);
animationDuration = ta.getInteger(R.styleable.SwitchButton_kswAnimationDuration, animationDuration);
fadeBack = ta.getBoolean(R.styleable.SwitchButton_kswFadeBack, true);
tintColor = ta.getColor(R.styleable.SwitchButton_kswTintColor, tintColor);
ta.recycle();
}
// thumb drawable and color
mThumbDrawable = thumbDrawable;
mThumbColor = thumbColor;
mIsThumbUseDrawable = mThumbDrawable != null;
mTintColor = tintCo