@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { mBackground.setBounds(0, 0, w, h); if (mIcon != null) { float half = mIconSize / 2f; mIcon.setBounds( (int) (mBackground.getCenterX() - half), (int) (mBackground.getCenterY() - half), (int) (mBackground.getCenterX() + half), (int) (mBackground.getCenterY() + half)); } if (mPrevIcon != null) { float half = mIconSize / 2f; mPrevIcon.setBounds( (int) (mBackground.getCenterX() - half), (int) (mBackground.getCenterY() - half), (int) (mBackground.getCenterX() + half), (int) (mBackground.getCenterY() + half)); } }
protected void applyStyle( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.FloatingActionButton, defStyleAttr, defStyleRes); int radius = -1; int elevation = -1; ColorStateList bgColor = null; int bgAnimDuration = -1; int iconSrc = 0; int iconLineMorphing = 0; for (int i = 0, count = a.getIndexCount(); i < count; i++) { int attr = a.getIndex(i); if (attr == R.styleable.FloatingActionButton_fab_radius) radius = a.getDimensionPixelSize(attr, 0); else if (attr == R.styleable.FloatingActionButton_fab_elevation) elevation = a.getDimensionPixelSize(attr, 0); else if (attr == R.styleable.FloatingActionButton_fab_backgroundColor) bgColor = a.getColorStateList(attr); else if (attr == R.styleable.FloatingActionButton_fab_backgroundAnimDuration) bgAnimDuration = a.getInteger(attr, 0); else if (attr == R.styleable.FloatingActionButton_fab_iconSrc) iconSrc = a.getResourceId(attr, 0); else if (attr == R.styleable.FloatingActionButton_fab_iconLineMorphing) iconLineMorphing = a.getResourceId(attr, 0); else if (attr == R.styleable.FloatingActionButton_fab_iconSize) mIconSize = a.getDimensionPixelSize(attr, 0); else if (attr == R.styleable.FloatingActionButton_fab_animDuration) mAnimDuration = a.getInteger(attr, 0); else if (attr == R.styleable.FloatingActionButton_fab_interpolator) { int resId = a.getResourceId(R.styleable.FloatingActionButton_fab_interpolator, 0); if (resId != 0) mInterpolator = AnimationUtils.loadInterpolator(context, resId); } } a.recycle(); if (mIconSize < 0) mIconSize = ThemeUtil.dpToPx(context, 24); if (mAnimDuration < 0) mAnimDuration = context.getResources().getInteger(android.R.integer.config_mediumAnimTime); if (mInterpolator == null) mInterpolator = new DecelerateInterpolator(); if (mBackground == null) { if (radius < 0) radius = ThemeUtil.dpToPx(context, 28); if (elevation < 0) elevation = ThemeUtil.dpToPx(context, 4); if (bgColor == null) bgColor = ColorStateList.valueOf(ThemeUtil.colorAccent(context, 0)); if (bgAnimDuration < 0) bgAnimDuration = 0; mBackground = new OvalShadowDrawable(radius, bgColor, elevation, elevation, bgAnimDuration); mBackground.setInEditMode(isInEditMode()); mBackground.setBounds(0, 0, getWidth(), getHeight()); mBackground.setCallback(this); } else { if (radius >= 0) mBackground.setRadius(radius); if (bgColor != null) mBackground.setColor(bgColor); if (elevation >= 0) mBackground.setShadow(elevation, elevation); if (bgAnimDuration >= 0) mBackground.setAnimationDuration(bgAnimDuration); } if (iconLineMorphing != 0) setIcon(new LineMorphingDrawable.Builder(context, iconLineMorphing).build(), false); else if (iconSrc != 0) setIcon(context.getResources().getDrawable(iconSrc), false); getRippleManager().onCreate(this, context, attrs, defStyleAttr, defStyleRes); Drawable background = getBackground(); if (background != null && background instanceof RippleDrawable) { RippleDrawable drawable = (RippleDrawable) background; drawable.setBackgroundDrawable(null); drawable.setMask( RippleDrawable.Mask.TYPE_OVAL, 0, 0, 0, 0, (int) mBackground.getPaddingLeft(), (int) mBackground.getPaddingTop(), (int) mBackground.getPaddingRight(), (int) mBackground.getPaddingBottom()); } }