protected void applyStyle( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { getRippleManager().onCreate(this, context, attrs, defStyleAttr, defStyleRes); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Switch, defStyleAttr, defStyleRes); for (int i = 0, count = a.getIndexCount(); i < count; i++) { int attr = a.getIndex(i); if (attr == R.styleable.Switch_sw_trackSize) mTrackSize = a.getDimensionPixelSize(attr, 0); else if (attr == R.styleable.Switch_sw_trackColor) mTrackColors = a.getColorStateList(attr); else if (attr == R.styleable.Switch_sw_trackCap) { int cap = a.getInteger(attr, 0); if (cap == 0) mTrackCap = Paint.Cap.BUTT; else if (cap == 1) mTrackCap = Paint.Cap.ROUND; else mTrackCap = Paint.Cap.SQUARE; } else if (attr == R.styleable.Switch_sw_thumbColor) mThumbColors = a.getColorStateList(attr); else if (attr == R.styleable.Switch_sw_thumbRadius) mThumbRadius = a.getDimensionPixelSize(attr, 0); else if (attr == R.styleable.Switch_sw_thumbElevation) { mShadowSize = a.getDimensionPixelSize(attr, 0); mShadowOffset = mShadowSize / 2; } else if (attr == R.styleable.Switch_sw_animDuration) mMaxAnimDuration = a.getInt(attr, 0); else if (attr == R.styleable.Switch_android_gravity) mGravity = a.getInt(attr, 0); else if (attr == R.styleable.Switch_android_checked) setCheckedImmediately(a.getBoolean(attr, mChecked)); else if (attr == R.styleable.Switch_sw_interpolator) { int resId = a.getResourceId(R.styleable.Switch_sw_interpolator, 0); if (resId != 0) mInterpolator = AnimationUtils.loadInterpolator(context, resId); } } a.recycle(); if (mTrackSize < 0) mTrackSize = ThemeUtil.dpToPx(context, 2); if (mThumbRadius < 0) mThumbRadius = ThemeUtil.dpToPx(context, 8); if (mShadowSize < 0) { mShadowSize = ThemeUtil.dpToPx(context, 2); mShadowOffset = mShadowSize / 2; } if (mMaxAnimDuration < 0) mMaxAnimDuration = context.getResources().getInteger(android.R.integer.config_mediumAnimTime); if (mInterpolator == null) mInterpolator = new DecelerateInterpolator(); if (mTrackColors == null) { int[][] states = new int[][] { new int[] {-android.R.attr.state_checked}, new int[] {android.R.attr.state_checked}, }; int[] colors = new int[] { ColorUtil.getColor(ThemeUtil.colorControlNormal(context, 0xFF000000), 0.5f), ColorUtil.getColor(ThemeUtil.colorControlActivated(context, 0xFF000000), 0.5f), }; mTrackColors = new ColorStateList(states, colors); } if (mThumbColors == null) { int[][] states = new int[][] { new int[] {-android.R.attr.state_checked}, new int[] {android.R.attr.state_checked}, }; int[] colors = new int[] { 0xFAFAFA, ThemeUtil.colorControlActivated(context, 0xFF000000), }; mThumbColors = new ColorStateList(states, colors); } mPaint.setStrokeCap(mTrackCap); buildShadow(); invalidate(); }