FloatingActionButton.java revision 6d7a9a02765e4cb497081e66dafb5d9fa76f4312
19840efe3dbdc7026521da8576574c55120782f6cChris Banes/* 29840efe3dbdc7026521da8576574c55120782f6cChris Banes * Copyright (C) 2015 The Android Open Source Project 39840efe3dbdc7026521da8576574c55120782f6cChris Banes * 49840efe3dbdc7026521da8576574c55120782f6cChris Banes * Licensed under the Apache License, Version 2.0 (the "License"); 59840efe3dbdc7026521da8576574c55120782f6cChris Banes * you may not use this file except in compliance with the License. 69840efe3dbdc7026521da8576574c55120782f6cChris Banes * You may obtain a copy of the License at 79840efe3dbdc7026521da8576574c55120782f6cChris Banes * 89840efe3dbdc7026521da8576574c55120782f6cChris Banes * http://www.apache.org/licenses/LICENSE-2.0 99840efe3dbdc7026521da8576574c55120782f6cChris Banes * 109840efe3dbdc7026521da8576574c55120782f6cChris Banes * Unless required by applicable law or agreed to in writing, software 119840efe3dbdc7026521da8576574c55120782f6cChris Banes * distributed under the License is distributed on an "AS IS" BASIS, 129840efe3dbdc7026521da8576574c55120782f6cChris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139840efe3dbdc7026521da8576574c55120782f6cChris Banes * See the License for the specific language governing permissions and 149840efe3dbdc7026521da8576574c55120782f6cChris Banes * limitations under the License. 159840efe3dbdc7026521da8576574c55120782f6cChris Banes */ 169840efe3dbdc7026521da8576574c55120782f6cChris Banes 179840efe3dbdc7026521da8576574c55120782f6cChris Banespackage android.support.design.widget; 189840efe3dbdc7026521da8576574c55120782f6cChris Banes 199840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.annotation.TargetApi; 209840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.content.Context; 219840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.content.res.ColorStateList; 229840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.content.res.TypedArray; 239840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.graphics.PorterDuff; 249840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.graphics.Rect; 259840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.graphics.drawable.Drawable; 269840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.os.Build; 277a13c8489daca7915623dd673df49de2d1a0bf30Chris Banesimport android.support.annotation.ColorInt; 28d9cbe69a6661315238d856abc22578d03666f63bChris Banesimport android.support.annotation.NonNull; 299840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.support.annotation.Nullable; 309840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.support.design.R; 310ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Weiimport android.support.design.widget.FloatingActionButtonImpl.InternalVisibilityChangedListener; 32b7f9224b1495db47eb8fd813b5912250e900770aChris Banesimport android.support.v4.view.ViewCompat; 339840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.util.AttributeSet; 34097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banesimport android.util.Log; 3514d064edb3e4a16a3b90a4a850560177bea1e60dChris Banesimport android.view.View; 369840efe3dbdc7026521da8576574c55120782f6cChris Banesimport android.widget.ImageView; 379840efe3dbdc7026521da8576574c55120782f6cChris Banes 38a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport java.util.List; 39b7f9224b1495db47eb8fd813b5912250e900770aChris Banes 409840efe3dbdc7026521da8576574c55120782f6cChris Banes/** 419840efe3dbdc7026521da8576574c55120782f6cChris Banes * Floating action buttons are used for a special type of promoted action. They are distinguished 4214d064edb3e4a16a3b90a4a850560177bea1e60dChris Banes * by a circled icon floating above the UI and have special motion behaviors related to morphing, 439840efe3dbdc7026521da8576574c55120782f6cChris Banes * launching, and the transferring anchor point. 449840efe3dbdc7026521da8576574c55120782f6cChris Banes * 459fb154338a62edc2c57dc036895199d6f1769400Chris Banes * <p>Floating action buttons come in two sizes: the default and the mini. The size can be 469fb154338a62edc2c57dc036895199d6f1769400Chris Banes * controlled with the {@code fabSize} attribute.</p> 479fb154338a62edc2c57dc036895199d6f1769400Chris Banes * 489fb154338a62edc2c57dc036895199d6f1769400Chris Banes * <p>As this class descends from {@link ImageView}, you can control the icon which is displayed 499fb154338a62edc2c57dc036895199d6f1769400Chris Banes * via {@link #setImageDrawable(Drawable)}.</p> 509fb154338a62edc2c57dc036895199d6f1769400Chris Banes * 519fb154338a62edc2c57dc036895199d6f1769400Chris Banes * <p>The background color of this view defaults to the your theme's {@code colorAccent}. If you 529fb154338a62edc2c57dc036895199d6f1769400Chris Banes * wish to change this at runtime then you can do so via 539fb154338a62edc2c57dc036895199d6f1769400Chris Banes * {@link #setBackgroundTintList(ColorStateList)}.</p> 549fb154338a62edc2c57dc036895199d6f1769400Chris Banes * 559fb154338a62edc2c57dc036895199d6f1769400Chris Banes * @attr ref android.support.design.R.styleable#FloatingActionButton_fabSize 569840efe3dbdc7026521da8576574c55120782f6cChris Banes */ 57b7f9224b1495db47eb8fd813b5912250e900770aChris Banes@CoordinatorLayout.DefaultBehavior(FloatingActionButton.Behavior.class) 58fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banespublic class FloatingActionButton extends VisibilityAwareImageButton { 599840efe3dbdc7026521da8576574c55120782f6cChris Banes 60097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes private static final String LOG_TAG = "FloatingActionButton"; 61097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes 620ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei /** 630ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * Callback to be invoked when the visibility of a FloatingActionButton changes. 640ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei */ 650ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei public abstract static class OnVisibilityChangedListener { 660ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei /** 670ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * Called when a FloatingActionButton has been 680ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * {@link #show(OnVisibilityChangedListener) shown}. 690ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * 700ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * @param fab the FloatingActionButton that was shown. 710ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei */ 720ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei public void onShown(FloatingActionButton fab) {} 730ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei 740ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei /** 750ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * Called when a FloatingActionButton has been 760ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * {@link #hide(OnVisibilityChangedListener) hidden}. 770ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * 780ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * @param fab the FloatingActionButton that was hidden. 790ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei */ 800ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei public void onHidden(FloatingActionButton fab) {} 810ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei } 820ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei 839840efe3dbdc7026521da8576574c55120782f6cChris Banes // These values must match those in the attrs declaration 849840efe3dbdc7026521da8576574c55120782f6cChris Banes private static final int SIZE_MINI = 1; 859840efe3dbdc7026521da8576574c55120782f6cChris Banes private static final int SIZE_NORMAL = 0; 869840efe3dbdc7026521da8576574c55120782f6cChris Banes 879840efe3dbdc7026521da8576574c55120782f6cChris Banes private ColorStateList mBackgroundTint; 889840efe3dbdc7026521da8576574c55120782f6cChris Banes private PorterDuff.Mode mBackgroundTintMode; 899840efe3dbdc7026521da8576574c55120782f6cChris Banes 90cd78954a2b32d9c22686f12c194fac7e49566cf6Chris Banes private int mBorderWidth; 919840efe3dbdc7026521da8576574c55120782f6cChris Banes private int mRippleColor; 929840efe3dbdc7026521da8576574c55120782f6cChris Banes private int mSize; 93d9cbe69a6661315238d856abc22578d03666f63bChris Banes private int mImagePadding; 949840efe3dbdc7026521da8576574c55120782f6cChris Banes 956d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes private boolean mCompatPadding; 969840efe3dbdc7026521da8576574c55120782f6cChris Banes private final Rect mShadowPadding; 979840efe3dbdc7026521da8576574c55120782f6cChris Banes 989840efe3dbdc7026521da8576574c55120782f6cChris Banes private final FloatingActionButtonImpl mImpl; 999840efe3dbdc7026521da8576574c55120782f6cChris Banes 1009840efe3dbdc7026521da8576574c55120782f6cChris Banes public FloatingActionButton(Context context) { 1019840efe3dbdc7026521da8576574c55120782f6cChris Banes this(context, null); 1029840efe3dbdc7026521da8576574c55120782f6cChris Banes } 1039840efe3dbdc7026521da8576574c55120782f6cChris Banes 1049840efe3dbdc7026521da8576574c55120782f6cChris Banes public FloatingActionButton(Context context, AttributeSet attrs) { 1059840efe3dbdc7026521da8576574c55120782f6cChris Banes this(context, attrs, 0); 1069840efe3dbdc7026521da8576574c55120782f6cChris Banes } 1079840efe3dbdc7026521da8576574c55120782f6cChris Banes 1089840efe3dbdc7026521da8576574c55120782f6cChris Banes public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) { 1099840efe3dbdc7026521da8576574c55120782f6cChris Banes super(context, attrs, defStyleAttr); 1109840efe3dbdc7026521da8576574c55120782f6cChris Banes 111809bb62055ad42b88f3a69308be222801b89fbd9Chris Banes ThemeUtils.checkAppCompatTheme(context); 112809bb62055ad42b88f3a69308be222801b89fbd9Chris Banes 1139840efe3dbdc7026521da8576574c55120782f6cChris Banes mShadowPadding = new Rect(); 1149840efe3dbdc7026521da8576574c55120782f6cChris Banes 1159840efe3dbdc7026521da8576574c55120782f6cChris Banes TypedArray a = context.obtainStyledAttributes(attrs, 1169840efe3dbdc7026521da8576574c55120782f6cChris Banes R.styleable.FloatingActionButton, defStyleAttr, 1179840efe3dbdc7026521da8576574c55120782f6cChris Banes R.style.Widget_Design_FloatingActionButton); 1189840efe3dbdc7026521da8576574c55120782f6cChris Banes mBackgroundTint = a.getColorStateList(R.styleable.FloatingActionButton_backgroundTint); 1199840efe3dbdc7026521da8576574c55120782f6cChris Banes mBackgroundTintMode = parseTintMode(a.getInt( 1209840efe3dbdc7026521da8576574c55120782f6cChris Banes R.styleable.FloatingActionButton_backgroundTintMode, -1), null); 1219840efe3dbdc7026521da8576574c55120782f6cChris Banes mRippleColor = a.getColor(R.styleable.FloatingActionButton_rippleColor, 0); 1229840efe3dbdc7026521da8576574c55120782f6cChris Banes mSize = a.getInt(R.styleable.FloatingActionButton_fabSize, SIZE_NORMAL); 123cd78954a2b32d9c22686f12c194fac7e49566cf6Chris Banes mBorderWidth = a.getDimensionPixelSize(R.styleable.FloatingActionButton_borderWidth, 0); 1249840efe3dbdc7026521da8576574c55120782f6cChris Banes final float elevation = a.getDimension(R.styleable.FloatingActionButton_elevation, 0f); 1259840efe3dbdc7026521da8576574c55120782f6cChris Banes final float pressedTranslationZ = a.getDimension( 1269840efe3dbdc7026521da8576574c55120782f6cChris Banes R.styleable.FloatingActionButton_pressedTranslationZ, 0f); 1276d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes mCompatPadding = a.getBoolean(R.styleable.FloatingActionButton_useCompatPadding, false); 1289840efe3dbdc7026521da8576574c55120782f6cChris Banes a.recycle(); 1299840efe3dbdc7026521da8576574c55120782f6cChris Banes 1309840efe3dbdc7026521da8576574c55120782f6cChris Banes final ShadowViewDelegate delegate = new ShadowViewDelegate() { 1319840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 1329840efe3dbdc7026521da8576574c55120782f6cChris Banes public float getRadius() { 1339840efe3dbdc7026521da8576574c55120782f6cChris Banes return getSizeDimension() / 2f; 1349840efe3dbdc7026521da8576574c55120782f6cChris Banes } 1359840efe3dbdc7026521da8576574c55120782f6cChris Banes 1369840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 1379840efe3dbdc7026521da8576574c55120782f6cChris Banes public void setShadowPadding(int left, int top, int right, int bottom) { 1389840efe3dbdc7026521da8576574c55120782f6cChris Banes mShadowPadding.set(left, top, right, bottom); 139d9cbe69a6661315238d856abc22578d03666f63bChris Banes setPadding(left + mImagePadding, top + mImagePadding, 140d9cbe69a6661315238d856abc22578d03666f63bChris Banes right + mImagePadding, bottom + mImagePadding); 1419840efe3dbdc7026521da8576574c55120782f6cChris Banes } 1429840efe3dbdc7026521da8576574c55120782f6cChris Banes 1439840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 1449840efe3dbdc7026521da8576574c55120782f6cChris Banes public void setBackgroundDrawable(Drawable background) { 1459840efe3dbdc7026521da8576574c55120782f6cChris Banes FloatingActionButton.super.setBackgroundDrawable(background); 1469840efe3dbdc7026521da8576574c55120782f6cChris Banes } 1476d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes 1486d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes @Override 1496d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes public boolean isCompatPaddingEnabled() { 1506d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes return mCompatPadding; 1516d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes } 1529840efe3dbdc7026521da8576574c55120782f6cChris Banes }; 1539840efe3dbdc7026521da8576574c55120782f6cChris Banes 154be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes final int sdk = Build.VERSION.SDK_INT; 155be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes if (sdk >= 21) { 1569840efe3dbdc7026521da8576574c55120782f6cChris Banes mImpl = new FloatingActionButtonLollipop(this, delegate); 1579ec922c215ed95a4bbd8bd7940e81dcfe6926893Chris Banes } else if (sdk >= 14) { 1589ec922c215ed95a4bbd8bd7940e81dcfe6926893Chris Banes mImpl = new FloatingActionButtonIcs(this, delegate); 1599840efe3dbdc7026521da8576574c55120782f6cChris Banes } else { 1609840efe3dbdc7026521da8576574c55120782f6cChris Banes mImpl = new FloatingActionButtonEclairMr1(this, delegate); 1619840efe3dbdc7026521da8576574c55120782f6cChris Banes } 1629840efe3dbdc7026521da8576574c55120782f6cChris Banes 163d9cbe69a6661315238d856abc22578d03666f63bChris Banes final int maxImageSize = (int) getResources().getDimension(R.dimen.design_fab_image_size); 164d9cbe69a6661315238d856abc22578d03666f63bChris Banes mImagePadding = (getSizeDimension() - maxImageSize) / 2; 1659840efe3dbdc7026521da8576574c55120782f6cChris Banes 166097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes mImpl.setBackgroundDrawable(mBackgroundTint, mBackgroundTintMode, 167097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes mRippleColor, mBorderWidth); 1689840efe3dbdc7026521da8576574c55120782f6cChris Banes mImpl.setElevation(elevation); 1699840efe3dbdc7026521da8576574c55120782f6cChris Banes mImpl.setPressedTranslationZ(pressedTranslationZ); 1706d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes mImpl.updatePadding(); 1719840efe3dbdc7026521da8576574c55120782f6cChris Banes } 1729840efe3dbdc7026521da8576574c55120782f6cChris Banes 1739840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 1749840efe3dbdc7026521da8576574c55120782f6cChris Banes protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 1759840efe3dbdc7026521da8576574c55120782f6cChris Banes final int preferredSize = getSizeDimension(); 1769840efe3dbdc7026521da8576574c55120782f6cChris Banes 1779840efe3dbdc7026521da8576574c55120782f6cChris Banes final int w = resolveAdjustedSize(preferredSize, widthMeasureSpec); 1789840efe3dbdc7026521da8576574c55120782f6cChris Banes final int h = resolveAdjustedSize(preferredSize, heightMeasureSpec); 1799840efe3dbdc7026521da8576574c55120782f6cChris Banes 1809840efe3dbdc7026521da8576574c55120782f6cChris Banes // As we want to stay circular, we set both dimensions to be the 1819840efe3dbdc7026521da8576574c55120782f6cChris Banes // smallest resolved dimension 1829840efe3dbdc7026521da8576574c55120782f6cChris Banes final int d = Math.min(w, h); 1839840efe3dbdc7026521da8576574c55120782f6cChris Banes 1849840efe3dbdc7026521da8576574c55120782f6cChris Banes // We add the shadow's padding to the measured dimension 1859840efe3dbdc7026521da8576574c55120782f6cChris Banes setMeasuredDimension( 1869840efe3dbdc7026521da8576574c55120782f6cChris Banes d + mShadowPadding.left + mShadowPadding.right, 1879840efe3dbdc7026521da8576574c55120782f6cChris Banes d + mShadowPadding.top + mShadowPadding.bottom); 1889840efe3dbdc7026521da8576574c55120782f6cChris Banes } 1899840efe3dbdc7026521da8576574c55120782f6cChris Banes 1909840efe3dbdc7026521da8576574c55120782f6cChris Banes /** 1919840efe3dbdc7026521da8576574c55120782f6cChris Banes * Set the ripple color for this {@link FloatingActionButton}. 1929840efe3dbdc7026521da8576574c55120782f6cChris Banes * <p> 1939840efe3dbdc7026521da8576574c55120782f6cChris Banes * When running on devices with KitKat or below, we draw a fill rather than a ripple. 1949840efe3dbdc7026521da8576574c55120782f6cChris Banes * 1959840efe3dbdc7026521da8576574c55120782f6cChris Banes * @param color ARGB color to use for the ripple. 1966d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * 1976d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @attr ref android.support.design.R.styleable#FloatingActionButton_rippleColor 1989840efe3dbdc7026521da8576574c55120782f6cChris Banes */ 1997a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes public void setRippleColor(@ColorInt int color) { 2009840efe3dbdc7026521da8576574c55120782f6cChris Banes if (mRippleColor != color) { 2019840efe3dbdc7026521da8576574c55120782f6cChris Banes mRippleColor = color; 2029840efe3dbdc7026521da8576574c55120782f6cChris Banes mImpl.setRippleColor(color); 2039840efe3dbdc7026521da8576574c55120782f6cChris Banes } 2049840efe3dbdc7026521da8576574c55120782f6cChris Banes } 2059840efe3dbdc7026521da8576574c55120782f6cChris Banes 2069840efe3dbdc7026521da8576574c55120782f6cChris Banes /** 2079840efe3dbdc7026521da8576574c55120782f6cChris Banes * Return the tint applied to the background drawable, if specified. 2089840efe3dbdc7026521da8576574c55120782f6cChris Banes * 2099840efe3dbdc7026521da8576574c55120782f6cChris Banes * @return the tint applied to the background drawable 2109840efe3dbdc7026521da8576574c55120782f6cChris Banes * @see #setBackgroundTintList(ColorStateList) 2119840efe3dbdc7026521da8576574c55120782f6cChris Banes */ 2129840efe3dbdc7026521da8576574c55120782f6cChris Banes @Nullable 2139840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 2149840efe3dbdc7026521da8576574c55120782f6cChris Banes public ColorStateList getBackgroundTintList() { 2159840efe3dbdc7026521da8576574c55120782f6cChris Banes return mBackgroundTint; 2169840efe3dbdc7026521da8576574c55120782f6cChris Banes } 2179840efe3dbdc7026521da8576574c55120782f6cChris Banes 2189840efe3dbdc7026521da8576574c55120782f6cChris Banes /** 2199840efe3dbdc7026521da8576574c55120782f6cChris Banes * Applies a tint to the background drawable. Does not modify the current tint 2209840efe3dbdc7026521da8576574c55120782f6cChris Banes * mode, which is {@link PorterDuff.Mode#SRC_IN} by default. 2219840efe3dbdc7026521da8576574c55120782f6cChris Banes * 2229840efe3dbdc7026521da8576574c55120782f6cChris Banes * @param tint the tint to apply, may be {@code null} to clear tint 2239840efe3dbdc7026521da8576574c55120782f6cChris Banes */ 2249840efe3dbdc7026521da8576574c55120782f6cChris Banes public void setBackgroundTintList(@Nullable ColorStateList tint) { 2257a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes if (mBackgroundTint != tint) { 2267a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes mBackgroundTint = tint; 2277a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes mImpl.setBackgroundTintList(tint); 2287a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes } 2299840efe3dbdc7026521da8576574c55120782f6cChris Banes } 2309840efe3dbdc7026521da8576574c55120782f6cChris Banes 2319840efe3dbdc7026521da8576574c55120782f6cChris Banes /** 2329840efe3dbdc7026521da8576574c55120782f6cChris Banes * Return the blending mode used to apply the tint to the background 2339840efe3dbdc7026521da8576574c55120782f6cChris Banes * drawable, if specified. 2349840efe3dbdc7026521da8576574c55120782f6cChris Banes * 2359840efe3dbdc7026521da8576574c55120782f6cChris Banes * @return the blending mode used to apply the tint to the background 2369840efe3dbdc7026521da8576574c55120782f6cChris Banes * drawable 2379840efe3dbdc7026521da8576574c55120782f6cChris Banes * @see #setBackgroundTintMode(PorterDuff.Mode) 2389840efe3dbdc7026521da8576574c55120782f6cChris Banes */ 2399840efe3dbdc7026521da8576574c55120782f6cChris Banes @Nullable 2409840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 2419840efe3dbdc7026521da8576574c55120782f6cChris Banes public PorterDuff.Mode getBackgroundTintMode() { 2429840efe3dbdc7026521da8576574c55120782f6cChris Banes return mBackgroundTintMode; 2439840efe3dbdc7026521da8576574c55120782f6cChris Banes } 2449840efe3dbdc7026521da8576574c55120782f6cChris Banes 2459840efe3dbdc7026521da8576574c55120782f6cChris Banes /** 2469840efe3dbdc7026521da8576574c55120782f6cChris Banes * Specifies the blending mode used to apply the tint specified by 2479840efe3dbdc7026521da8576574c55120782f6cChris Banes * {@link #setBackgroundTintList(ColorStateList)}} to the background 2489840efe3dbdc7026521da8576574c55120782f6cChris Banes * drawable. The default mode is {@link PorterDuff.Mode#SRC_IN}. 2499840efe3dbdc7026521da8576574c55120782f6cChris Banes * 2509840efe3dbdc7026521da8576574c55120782f6cChris Banes * @param tintMode the blending mode used to apply the tint, may be 2519840efe3dbdc7026521da8576574c55120782f6cChris Banes * {@code null} to clear tint 2529840efe3dbdc7026521da8576574c55120782f6cChris Banes */ 2539840efe3dbdc7026521da8576574c55120782f6cChris Banes public void setBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { 2547a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes if (mBackgroundTintMode != tintMode) { 2557a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes mBackgroundTintMode = tintMode; 2567a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes mImpl.setBackgroundTintMode(tintMode); 2577a13c8489daca7915623dd673df49de2d1a0bf30Chris Banes } 2589840efe3dbdc7026521da8576574c55120782f6cChris Banes } 2599840efe3dbdc7026521da8576574c55120782f6cChris Banes 2609840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 261097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes public void setBackgroundDrawable(Drawable background) { 262097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes Log.i(LOG_TAG, "Setting a custom background is not supported."); 263097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes } 264097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes 265097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes @Override 266097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes public void setBackgroundResource(int resid) { 267097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes Log.i(LOG_TAG, "Setting a custom background is not supported."); 268097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes } 269097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes 270097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes @Override 271097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes public void setBackgroundColor(int color) { 272097e80a3c5518c6bf2e9f3f9b55ed9f4b5cc37e8Chris Banes Log.i(LOG_TAG, "Setting a custom background is not supported."); 273be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 274be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes 275be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes /** 276be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes * Shows the button. 2778c05e5f52fbc790b745e768398d9e69d6b9d9ee1Chris Banes * <p>This method will animate the button show if the view has already been laid out.</p> 278be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes */ 279be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes public void show() { 280fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes show(null); 2810ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei } 2820ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei 2830ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei /** 2840ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * Shows the button. 2858c05e5f52fbc790b745e768398d9e69d6b9d9ee1Chris Banes * <p>This method will animate the button show if the view has already been laid out.</p> 2860ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * 2870ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * @param listener the listener to notify when this view is shown 2880ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei */ 2890ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei public void show(@Nullable final OnVisibilityChangedListener listener) { 290fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes show(listener, true); 291fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes } 292fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes 293fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes private void show(OnVisibilityChangedListener listener, boolean fromUser) { 294fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes mImpl.show(wrapOnVisibilityChangedListener(listener), fromUser); 295be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 296be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes 297be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes /** 298be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes * Hides the button. 299be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes * <p>This method will animate the button hide if the view has already been laid out.</p> 300be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes */ 301be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes public void hide() { 302fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes hide(null); 3030ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei } 3040ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei 3050ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei /** 3060ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * Hides the button. 3070ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * <p>This method will animate the button hide if the view has already been laid out.</p> 3080ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * 3090ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei * @param listener the listener to notify when this view is hidden 3100ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei */ 3110ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei public void hide(@Nullable OnVisibilityChangedListener listener) { 312fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes hide(listener, true); 313fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes } 314fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes 315fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes private void hide(@Nullable OnVisibilityChangedListener listener, boolean fromUser) { 316fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes mImpl.hide(wrapOnVisibilityChangedListener(listener), fromUser); 3170ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei } 3180ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei 3196d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes /** 3206d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * Set whether FloatingActionButton should add inner padding on platforms Lollipop and after, 3216d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * to ensure consistent dimensions on all platforms. 3226d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * 3236d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @param useCompatPadding true if FloatingActionButton is adding inner padding on platforms 3246d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * Lollipop and after, to ensure consistent dimensions on all platforms. 3256d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * 3266d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @attr ref android.support.design.R.styleable#FloatingActionButton_useCompatPadding 3276d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @see #getUseCompatPadding() 3286d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes */ 3296d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes public void setUseCompatPadding(boolean useCompatPadding) { 3306d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes if (mCompatPadding != useCompatPadding) { 3316d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes mCompatPadding = useCompatPadding; 3326d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes mImpl.onCompatShadowChanged(); 3336d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes } 3346d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes } 3356d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes 3366d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes /** 3376d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * Returns whether FloatingActionButton will add inner padding on platforms Lollipop and after. 3386d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * 3396d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @return true if FloatingActionButton is adding inner padding on platforms Lollipop and after, 3406d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * to ensure consistent dimensions on all platforms. 3416d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * 3426d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @attr ref android.support.design.R.styleable#FloatingActionButton_useCompatPadding 3436d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @see #setUseCompatPadding(boolean) 3446d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes */ 3456d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes public boolean getUseCompatPadding() { 3466d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes return mCompatPadding; 3476d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes } 3486d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes 3490ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei @Nullable 3500ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei private InternalVisibilityChangedListener wrapOnVisibilityChangedListener( 3510ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei @Nullable final OnVisibilityChangedListener listener) { 3520ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei if (listener == null) { 3530ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei return null; 3540ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei } 3550ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei 3560ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei return new InternalVisibilityChangedListener() { 3570ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei @Override 3580ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei public void onShown() { 3590ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei listener.onShown(FloatingActionButton.this); 3600ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei } 3610ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei 3620ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei @Override 3630ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei public void onHidden() { 3640ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei listener.onHidden(FloatingActionButton.this); 3650ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei } 3660ad7ef59b28d8ffafd551d2756b5a8ec47c90682Mark Wei }; 3679840efe3dbdc7026521da8576574c55120782f6cChris Banes } 3689840efe3dbdc7026521da8576574c55120782f6cChris Banes 3699840efe3dbdc7026521da8576574c55120782f6cChris Banes final int getSizeDimension() { 3709840efe3dbdc7026521da8576574c55120782f6cChris Banes switch (mSize) { 3719840efe3dbdc7026521da8576574c55120782f6cChris Banes case SIZE_MINI: 372a577676a64e5353b8ec927117151aa6be84adf66Chris Banes return getResources().getDimensionPixelSize(R.dimen.design_fab_size_mini); 3739840efe3dbdc7026521da8576574c55120782f6cChris Banes case SIZE_NORMAL: 3749840efe3dbdc7026521da8576574c55120782f6cChris Banes default: 375a577676a64e5353b8ec927117151aa6be84adf66Chris Banes return getResources().getDimensionPixelSize(R.dimen.design_fab_size_normal); 3769840efe3dbdc7026521da8576574c55120782f6cChris Banes } 3779840efe3dbdc7026521da8576574c55120782f6cChris Banes } 3789840efe3dbdc7026521da8576574c55120782f6cChris Banes 3799840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 380d9770e12c8ff2d4417700492c6616572be897e93Chris Banes protected void onAttachedToWindow() { 381d9770e12c8ff2d4417700492c6616572be897e93Chris Banes super.onAttachedToWindow(); 382d9770e12c8ff2d4417700492c6616572be897e93Chris Banes mImpl.onAttachedToWindow(); 383d9770e12c8ff2d4417700492c6616572be897e93Chris Banes } 384d9770e12c8ff2d4417700492c6616572be897e93Chris Banes 385d9770e12c8ff2d4417700492c6616572be897e93Chris Banes @Override 386d9770e12c8ff2d4417700492c6616572be897e93Chris Banes protected void onDetachedFromWindow() { 387d9770e12c8ff2d4417700492c6616572be897e93Chris Banes super.onDetachedFromWindow(); 388d9770e12c8ff2d4417700492c6616572be897e93Chris Banes mImpl.onDetachedFromWindow(); 389d9770e12c8ff2d4417700492c6616572be897e93Chris Banes } 390d9770e12c8ff2d4417700492c6616572be897e93Chris Banes 391d9770e12c8ff2d4417700492c6616572be897e93Chris Banes @Override 3929840efe3dbdc7026521da8576574c55120782f6cChris Banes protected void drawableStateChanged() { 3939840efe3dbdc7026521da8576574c55120782f6cChris Banes super.drawableStateChanged(); 3949840efe3dbdc7026521da8576574c55120782f6cChris Banes mImpl.onDrawableStateChanged(getDrawableState()); 3959840efe3dbdc7026521da8576574c55120782f6cChris Banes } 3969840efe3dbdc7026521da8576574c55120782f6cChris Banes 3979840efe3dbdc7026521da8576574c55120782f6cChris Banes @TargetApi(Build.VERSION_CODES.HONEYCOMB) 3989840efe3dbdc7026521da8576574c55120782f6cChris Banes @Override 3999840efe3dbdc7026521da8576574c55120782f6cChris Banes public void jumpDrawablesToCurrentState() { 4009840efe3dbdc7026521da8576574c55120782f6cChris Banes super.jumpDrawablesToCurrentState(); 4019840efe3dbdc7026521da8576574c55120782f6cChris Banes mImpl.jumpDrawableToCurrentState(); 4029840efe3dbdc7026521da8576574c55120782f6cChris Banes } 4039840efe3dbdc7026521da8576574c55120782f6cChris Banes 404d9cbe69a6661315238d856abc22578d03666f63bChris Banes /** 405d9cbe69a6661315238d856abc22578d03666f63bChris Banes * Return in {@code rect} the bounds of the actual floating action button content in view-local 406d9cbe69a6661315238d856abc22578d03666f63bChris Banes * coordinates. This is defined as anything within any visible shadow. 407d9cbe69a6661315238d856abc22578d03666f63bChris Banes * 408d9cbe69a6661315238d856abc22578d03666f63bChris Banes * @return true if this view actually has been laid out and has a content rect, else false. 409d9cbe69a6661315238d856abc22578d03666f63bChris Banes */ 410d9cbe69a6661315238d856abc22578d03666f63bChris Banes public boolean getContentRect(@NonNull Rect rect) { 411d9cbe69a6661315238d856abc22578d03666f63bChris Banes if (ViewCompat.isLaidOut(this)) { 412d9cbe69a6661315238d856abc22578d03666f63bChris Banes rect.set(0, 0, getWidth(), getHeight()); 413d9cbe69a6661315238d856abc22578d03666f63bChris Banes rect.left += mShadowPadding.left; 414d9cbe69a6661315238d856abc22578d03666f63bChris Banes rect.top += mShadowPadding.top; 415d9cbe69a6661315238d856abc22578d03666f63bChris Banes rect.right -= mShadowPadding.right; 416d9cbe69a6661315238d856abc22578d03666f63bChris Banes rect.bottom -= mShadowPadding.bottom; 417d9cbe69a6661315238d856abc22578d03666f63bChris Banes return true; 418d9cbe69a6661315238d856abc22578d03666f63bChris Banes } else { 419d9cbe69a6661315238d856abc22578d03666f63bChris Banes return false; 420d9cbe69a6661315238d856abc22578d03666f63bChris Banes } 421d9cbe69a6661315238d856abc22578d03666f63bChris Banes } 422d9cbe69a6661315238d856abc22578d03666f63bChris Banes 4233d81c900316412b4130bf40e0dd8b0d3d3a93e78Chris Banes /** 4243d81c900316412b4130bf40e0dd8b0d3d3a93e78Chris Banes * Returns the FloatingActionButton's background, minus any compatible shadow implementation. 4253d81c900316412b4130bf40e0dd8b0d3d3a93e78Chris Banes */ 4263d81c900316412b4130bf40e0dd8b0d3d3a93e78Chris Banes @NonNull 4273d81c900316412b4130bf40e0dd8b0d3d3a93e78Chris Banes public Drawable getContentBackground() { 4283d81c900316412b4130bf40e0dd8b0d3d3a93e78Chris Banes return mImpl.getContentBackground(); 4293d81c900316412b4130bf40e0dd8b0d3d3a93e78Chris Banes } 4303d81c900316412b4130bf40e0dd8b0d3d3a93e78Chris Banes 4319840efe3dbdc7026521da8576574c55120782f6cChris Banes private static int resolveAdjustedSize(int desiredSize, int measureSpec) { 4329840efe3dbdc7026521da8576574c55120782f6cChris Banes int result = desiredSize; 4339840efe3dbdc7026521da8576574c55120782f6cChris Banes int specMode = MeasureSpec.getMode(measureSpec); 4349840efe3dbdc7026521da8576574c55120782f6cChris Banes int specSize = MeasureSpec.getSize(measureSpec); 4359840efe3dbdc7026521da8576574c55120782f6cChris Banes switch (specMode) { 4369840efe3dbdc7026521da8576574c55120782f6cChris Banes case MeasureSpec.UNSPECIFIED: 4379840efe3dbdc7026521da8576574c55120782f6cChris Banes // Parent says we can be as big as we want. Just don't be larger 4389840efe3dbdc7026521da8576574c55120782f6cChris Banes // than max size imposed on ourselves. 4399840efe3dbdc7026521da8576574c55120782f6cChris Banes result = desiredSize; 4409840efe3dbdc7026521da8576574c55120782f6cChris Banes break; 4419840efe3dbdc7026521da8576574c55120782f6cChris Banes case MeasureSpec.AT_MOST: 4429840efe3dbdc7026521da8576574c55120782f6cChris Banes // Parent says we can be as big as we want, up to specSize. 4439840efe3dbdc7026521da8576574c55120782f6cChris Banes // Don't be larger than specSize, and don't be larger than 4449840efe3dbdc7026521da8576574c55120782f6cChris Banes // the max size imposed on ourselves. 4459840efe3dbdc7026521da8576574c55120782f6cChris Banes result = Math.min(desiredSize, specSize); 4469840efe3dbdc7026521da8576574c55120782f6cChris Banes break; 4479840efe3dbdc7026521da8576574c55120782f6cChris Banes case MeasureSpec.EXACTLY: 4489840efe3dbdc7026521da8576574c55120782f6cChris Banes // No choice. Do what we are told. 4499840efe3dbdc7026521da8576574c55120782f6cChris Banes result = specSize; 4509840efe3dbdc7026521da8576574c55120782f6cChris Banes break; 4519840efe3dbdc7026521da8576574c55120782f6cChris Banes } 4529840efe3dbdc7026521da8576574c55120782f6cChris Banes return result; 4539840efe3dbdc7026521da8576574c55120782f6cChris Banes } 4549840efe3dbdc7026521da8576574c55120782f6cChris Banes 4559840efe3dbdc7026521da8576574c55120782f6cChris Banes static PorterDuff.Mode parseTintMode(int value, PorterDuff.Mode defaultMode) { 4569840efe3dbdc7026521da8576574c55120782f6cChris Banes switch (value) { 4579840efe3dbdc7026521da8576574c55120782f6cChris Banes case 3: 4589840efe3dbdc7026521da8576574c55120782f6cChris Banes return PorterDuff.Mode.SRC_OVER; 4599840efe3dbdc7026521da8576574c55120782f6cChris Banes case 5: 4609840efe3dbdc7026521da8576574c55120782f6cChris Banes return PorterDuff.Mode.SRC_IN; 4619840efe3dbdc7026521da8576574c55120782f6cChris Banes case 9: 4629840efe3dbdc7026521da8576574c55120782f6cChris Banes return PorterDuff.Mode.SRC_ATOP; 4639840efe3dbdc7026521da8576574c55120782f6cChris Banes case 14: 4649840efe3dbdc7026521da8576574c55120782f6cChris Banes return PorterDuff.Mode.MULTIPLY; 4659840efe3dbdc7026521da8576574c55120782f6cChris Banes case 15: 4669840efe3dbdc7026521da8576574c55120782f6cChris Banes return PorterDuff.Mode.SCREEN; 4679840efe3dbdc7026521da8576574c55120782f6cChris Banes default: 4689840efe3dbdc7026521da8576574c55120782f6cChris Banes return defaultMode; 4699840efe3dbdc7026521da8576574c55120782f6cChris Banes } 4709840efe3dbdc7026521da8576574c55120782f6cChris Banes } 47114d064edb3e4a16a3b90a4a850560177bea1e60dChris Banes 47214d064edb3e4a16a3b90a4a850560177bea1e60dChris Banes /** 473b7f9224b1495db47eb8fd813b5912250e900770aChris Banes * Behavior designed for use with {@link FloatingActionButton} instances. It's main function 474b7f9224b1495db47eb8fd813b5912250e900770aChris Banes * is to move {@link FloatingActionButton} views so that any displayed {@link Snackbar}s do 475b7f9224b1495db47eb8fd813b5912250e900770aChris Banes * not cover them. 476b7f9224b1495db47eb8fd813b5912250e900770aChris Banes */ 477b7f9224b1495db47eb8fd813b5912250e900770aChris Banes public static class Behavior extends CoordinatorLayout.Behavior<FloatingActionButton> { 478b7f9224b1495db47eb8fd813b5912250e900770aChris Banes // We only support the FAB <> Snackbar shift movement on Honeycomb and above. This is 479b7f9224b1495db47eb8fd813b5912250e900770aChris Banes // because we can use view translation properties which greatly simplifies the code. 480b7f9224b1495db47eb8fd813b5912250e900770aChris Banes private static final boolean SNACKBAR_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11; 481b7f9224b1495db47eb8fd813b5912250e900770aChris Banes 482a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes private ValueAnimatorCompat mFabTranslationYAnimator; 48318d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes private float mFabTranslationY; 484a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes private Rect mTmpRect; 485b7f9224b1495db47eb8fd813b5912250e900770aChris Banes 486b7f9224b1495db47eb8fd813b5912250e900770aChris Banes @Override 487b7f9224b1495db47eb8fd813b5912250e900770aChris Banes public boolean layoutDependsOn(CoordinatorLayout parent, 488be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes FloatingActionButton child, View dependency) { 489a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes // We're dependent on all SnackbarLayouts (if enabled) 490a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout; 491b7f9224b1495db47eb8fd813b5912250e900770aChris Banes } 492b7f9224b1495db47eb8fd813b5912250e900770aChris Banes 493b7f9224b1495db47eb8fd813b5912250e900770aChris Banes @Override 494b7f9224b1495db47eb8fd813b5912250e900770aChris Banes public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, 495a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes View dependency) { 496a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes if (dependency instanceof Snackbar.SnackbarLayout) { 497a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes updateFabTranslationForSnackbar(parent, child, dependency); 498a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes } else if (dependency instanceof AppBarLayout) { 499be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // If we're depending on an AppBarLayout we will show/hide it automatically 500be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // if the FAB is anchored to the AppBarLayout 501be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes updateFabVisibility(parent, (AppBarLayout) dependency, child); 502be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 503be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes return false; 504be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 505a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes 506be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes private boolean updateFabVisibility(CoordinatorLayout parent, 507be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes AppBarLayout appBarLayout, FloatingActionButton child) { 508be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes final CoordinatorLayout.LayoutParams lp = 509be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes (CoordinatorLayout.LayoutParams) child.getLayoutParams(); 510be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes if (lp.getAnchorId() != appBarLayout.getId()) { 511be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // The anchor ID doesn't match the dependency, so we won't automatically 512be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // show/hide the FAB 513be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes return false; 514be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 515a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes 516fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes if (child.getUserSetVisibility() != VISIBLE) { 517fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes // The view isn't set to be visible so skip changing it's visibility 518fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes return false; 519fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes } 520fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes 521be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes if (mTmpRect == null) { 522be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes mTmpRect = new Rect(); 523a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes } 524be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes 525be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // First, let's get the visible rect of the dependency 526be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes final Rect rect = mTmpRect; 527be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes ViewGroupUtils.getDescendantRect(parent, appBarLayout, rect); 528be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes 529be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes if (rect.bottom <= appBarLayout.getMinimumHeightForVisibleOverlappingContent()) { 530be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // If the anchor's bottom is below the seam, we'll animate our FAB out 531fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes child.hide(null, false); 532be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } else { 533be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // Else, we'll animate our FAB back in 534fc780bab91bd4275ae2c3b75c3dfb327e008e4dbChris Banes child.show(null, false); 535be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 536be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes return true; 537b7f9224b1495db47eb8fd813b5912250e900770aChris Banes } 538b7f9224b1495db47eb8fd813b5912250e900770aChris Banes 539a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes private void updateFabTranslationForSnackbar(CoordinatorLayout parent, 540a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes final FloatingActionButton fab, View snackbar) { 541be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes if (fab.getVisibility() != View.VISIBLE) { 542be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes return; 543be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 544be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes 54518d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes final float targetTransY = getFabTranslationYForSnackbar(parent, fab); 54618d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes if (mFabTranslationY == targetTransY) { 54718d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes // We're already at (or currently animating to) the target value, return... 54818d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes return; 54918d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes } 55018d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes 55118d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes final float currentTransY = ViewCompat.getTranslationY(fab); 55218d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes 553a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes // Make sure that any current animation is cancelled 554a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes if (mFabTranslationYAnimator != null && mFabTranslationYAnimator.isRunning()) { 555a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes mFabTranslationYAnimator.cancel(); 556a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes } 557a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes 558a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes if (Math.abs(currentTransY - targetTransY) > (fab.getHeight() * 0.667f)) { 55918d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes // If the FAB will be travelling by more than 2/3 of it's height, let's animate 56018d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes // it instead 561a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes if (mFabTranslationYAnimator == null) { 562a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes mFabTranslationYAnimator = ViewUtils.createAnimator(); 563a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes mFabTranslationYAnimator.setInterpolator( 564a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR); 565a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes mFabTranslationYAnimator.setUpdateListener( 566a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes new ValueAnimatorCompat.AnimatorUpdateListener() { 567a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes @Override 568a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes public void onAnimationUpdate(ValueAnimatorCompat animator) { 569a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes ViewCompat.setTranslationY(fab, 570a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes animator.getAnimatedFloatValue()); 571a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes } 572a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes }); 573a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes } 574a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes mFabTranslationYAnimator.setFloatValues(currentTransY, targetTransY); 575a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes mFabTranslationYAnimator.start(); 57618d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes } else { 57718d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes // Now update the translation Y 57818d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes ViewCompat.setTranslationY(fab, targetTransY); 57918d22257ccfb5cebb3ccd2450736e735ed1fb9bbChris Banes } 580a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes 581a419ee1ef9aef8b567f1ccd8c29d01ec7bff4cc9Chris Banes mFabTranslationY = targetTransY; 582b7f9224b1495db47eb8fd813b5912250e900770aChris Banes } 583b7f9224b1495db47eb8fd813b5912250e900770aChris Banes 584a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes private float getFabTranslationYForSnackbar(CoordinatorLayout parent, 585a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes FloatingActionButton fab) { 586b7f9224b1495db47eb8fd813b5912250e900770aChris Banes float minOffset = 0; 587a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes final List<View> dependencies = parent.getDependencies(fab); 588a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes for (int i = 0, z = dependencies.size(); i < z; i++) { 589a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes final View view = dependencies.get(i); 590a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(fab, view)) { 591a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes minOffset = Math.min(minOffset, 592a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes ViewCompat.getTranslationY(view) - view.getHeight()); 593b7f9224b1495db47eb8fd813b5912250e900770aChris Banes } 594b7f9224b1495db47eb8fd813b5912250e900770aChris Banes } 595a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes 596b7f9224b1495db47eb8fd813b5912250e900770aChris Banes return minOffset; 597b7f9224b1495db47eb8fd813b5912250e900770aChris Banes } 598b7f9224b1495db47eb8fd813b5912250e900770aChris Banes 599e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes @Override 600e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes public boolean onLayoutChild(CoordinatorLayout parent, FloatingActionButton child, 601e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes int layoutDirection) { 602be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // First, lets make sure that the visibility of the FAB is consistent 603be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes final List<View> dependencies = parent.getDependencies(child); 604be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes for (int i = 0, count = dependencies.size(); i < count; i++) { 605be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes final View dependency = dependencies.get(i); 606be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes if (dependency instanceof AppBarLayout 607be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes && updateFabVisibility(parent, (AppBarLayout) dependency, child)) { 608be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes break; 609be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 610be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes } 611be48ed9161c09c4b2178ab6dbe28638222809fc7Chris Banes // Now let the CoordinatorLayout lay out the FAB 612e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes parent.onLayoutChild(child, layoutDirection); 613e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes // Now offset it if needed 614e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes offsetIfNeeded(parent, child); 615e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes return true; 616e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes } 617e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes 618e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes /** 619e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes * Pre-Lollipop we use padding so that the shadow has enough space to be drawn. This method 620e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes * offsets our layout position so that we're positioned correctly if we're on one of 621e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes * our parent's edges. 622e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes */ 623e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes private void offsetIfNeeded(CoordinatorLayout parent, FloatingActionButton fab) { 624e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes final Rect padding = fab.mShadowPadding; 625e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes 626e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes if (padding != null && padding.centerX() > 0 && padding.centerY() > 0) { 627e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes final CoordinatorLayout.LayoutParams lp = 628e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); 629e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes 630e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes int offsetTB = 0, offsetLR = 0; 631e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes 632e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes if (fab.getRight() >= parent.getWidth() - lp.rightMargin) { 633e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes // If we're on the left edge, shift it the right 634e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes offsetLR = padding.right; 635e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes } else if (fab.getLeft() <= lp.leftMargin) { 636e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes // If we're on the left edge, shift it the left 637e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes offsetLR = -padding.left; 638e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes } 639e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes if (fab.getBottom() >= parent.getBottom() - lp.bottomMargin) { 640e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes // If we're on the bottom edge, shift it down 641e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes offsetTB = padding.bottom; 642e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes } else if (fab.getTop() <= lp.topMargin) { 643e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes // If we're on the top edge, shift it up 644e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes offsetTB = -padding.top; 645e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes } 646e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes 647e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes fab.offsetTopAndBottom(offsetTB); 648e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes fab.offsetLeftAndRight(offsetLR); 649e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes } 650e882ef3492de3d2bb687b454e08b870b06d8f4e2Chris Banes } 651b7f9224b1495db47eb8fd813b5912250e900770aChris Banes } 6526d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes 6536d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes /** 6546d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * Returns the backward compatible elevation of the FloatingActionButton. 6556d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * 6566d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @returns the backward compatible elevation in pixels. 6576d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @attr ref android.support.design.R.styleable#FloatingActionButton_elevation 6586d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @see #setFloatingActionButtonElevation(float) 6596d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes */ 6606d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes public float getFloatingActionButtonElevation() { 6616d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes return mImpl.getElevation(); 6626d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes } 6636d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes 6646d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes /** 6656d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * Updates the backward compatible elevation of the FloatingActionButton. 6666d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * 6676d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @param elevation The backward compatible elevation in pixels. 6686d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @attr ref android.support.design.R.styleable#FloatingActionButton_elevation 6696d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @see #getFloatingActionButtonElevation() 6706d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes * @see #setUseCompatPadding(boolean) 6716d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes */ 6726d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes public void setFloatingActionButtonElevation(float elevation) { 6736d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes mImpl.setElevation(elevation); 6746d7a9a02765e4cb497081e66dafb5d9fa76f4312Chris Banes } 6759840efe3dbdc7026521da8576574c55120782f6cChris Banes} 676