FloatingActionButtonImpl.java revision fc780bab91bd4275ae2c3b75c3dfb327e008e4db
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.design.widget; 18 19import android.content.res.ColorStateList; 20import android.content.res.Resources; 21import android.graphics.Color; 22import android.graphics.PorterDuff; 23import android.graphics.drawable.Drawable; 24import android.graphics.drawable.GradientDrawable; 25import android.support.annotation.Nullable; 26import android.support.design.R; 27import android.view.ViewTreeObserver; 28 29abstract class FloatingActionButtonImpl { 30 31 interface InternalVisibilityChangedListener { 32 public void onShown(); 33 public void onHidden(); 34 } 35 36 static final int SHOW_HIDE_ANIM_DURATION = 200; 37 38 static final int[] PRESSED_ENABLED_STATE_SET = {android.R.attr.state_pressed, 39 android.R.attr.state_enabled}; 40 static final int[] FOCUSED_ENABLED_STATE_SET = {android.R.attr.state_focused, 41 android.R.attr.state_enabled}; 42 static final int[] EMPTY_STATE_SET = new int[0]; 43 44 final VisibilityAwareImageButton mView; 45 final ShadowViewDelegate mShadowViewDelegate; 46 47 private ViewTreeObserver.OnPreDrawListener mPreDrawListener; 48 49 FloatingActionButtonImpl(VisibilityAwareImageButton view, 50 ShadowViewDelegate shadowViewDelegate) { 51 mView = view; 52 mShadowViewDelegate = shadowViewDelegate; 53 } 54 55 abstract void setBackgroundDrawable(ColorStateList backgroundTint, 56 PorterDuff.Mode backgroundTintMode, int rippleColor, int borderWidth); 57 58 abstract void setBackgroundTintList(ColorStateList tint); 59 60 abstract void setBackgroundTintMode(PorterDuff.Mode tintMode); 61 62 abstract void setRippleColor(int rippleColor); 63 64 abstract void setElevation(float elevation); 65 66 abstract void setPressedTranslationZ(float translationZ); 67 68 abstract void onDrawableStateChanged(int[] state); 69 70 abstract void jumpDrawableToCurrentState(); 71 72 abstract void hide(@Nullable InternalVisibilityChangedListener listener, boolean fromUser); 73 74 abstract void show(@Nullable InternalVisibilityChangedListener listener, boolean fromUser); 75 76 void onAttachedToWindow() { 77 if (requirePreDrawListener()) { 78 ensurePreDrawListener(); 79 mView.getViewTreeObserver().addOnPreDrawListener(mPreDrawListener); 80 } 81 } 82 83 void onDetachedFromWindow() { 84 if (mPreDrawListener != null) { 85 mView.getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener); 86 mPreDrawListener = null; 87 } 88 } 89 90 boolean requirePreDrawListener() { 91 return false; 92 } 93 94 CircularBorderDrawable createBorderDrawable(int borderWidth, ColorStateList backgroundTint) { 95 final Resources resources = mView.getResources(); 96 CircularBorderDrawable borderDrawable = newCircularDrawable(); 97 borderDrawable.setGradientColors( 98 resources.getColor(R.color.design_fab_stroke_top_outer_color), 99 resources.getColor(R.color.design_fab_stroke_top_inner_color), 100 resources.getColor(R.color.design_fab_stroke_end_inner_color), 101 resources.getColor(R.color.design_fab_stroke_end_outer_color)); 102 borderDrawable.setBorderWidth(borderWidth); 103 borderDrawable.setBorderTint(backgroundTint); 104 return borderDrawable; 105 } 106 107 CircularBorderDrawable newCircularDrawable() { 108 return new CircularBorderDrawable(); 109 } 110 111 void onPreDraw() { 112 } 113 114 private void ensurePreDrawListener() { 115 if (mPreDrawListener == null) { 116 mPreDrawListener = new ViewTreeObserver.OnPreDrawListener() { 117 @Override 118 public boolean onPreDraw() { 119 FloatingActionButtonImpl.this.onPreDraw(); 120 return true; 121 } 122 }; 123 } 124 } 125 126 GradientDrawable createShapeDrawable() { 127 GradientDrawable d = new GradientDrawable(); 128 d.setShape(GradientDrawable.OVAL); 129 d.setColor(Color.WHITE); 130 return d; 131 } 132} 133