OutlineOnlyWithChildrenFrameLayout.java revision 8e10080c914d1ad0784394fa3026b85535535847
1/* 2 * Copyright (C) 2016 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.v17.internal.widget; 18 19import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21import android.annotation.TargetApi; 22import android.content.Context; 23import android.graphics.Outline; 24import android.support.annotation.RequiresApi; 25import android.support.annotation.RestrictTo; 26import android.util.AttributeSet; 27import android.view.View; 28import android.view.ViewOutlineProvider; 29import android.widget.FrameLayout; 30 31/** 32 * {@link FrameLayout} subclass that provides an outline only when it has children, so that it does 33 * not cast a shadow when empty. 34 * 35 * @hide 36 */ 37@RequiresApi(21) 38@TargetApi(21) 39@RestrictTo(LIBRARY_GROUP) 40public class OutlineOnlyWithChildrenFrameLayout extends FrameLayout { 41 42 private ViewOutlineProvider mMagicalOutlineProvider; 43 private ViewOutlineProvider mInnerOutlineProvider; 44 45 public OutlineOnlyWithChildrenFrameLayout(Context context) { 46 super(context); 47 } 48 49 public OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 } 52 53 public OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs, 54 int defStyleAttr) { 55 super(context, attrs, defStyleAttr); 56 } 57 58 public OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs, 59 int defStyleAttr, int defStyleRes) { 60 super(context, attrs, defStyleAttr, defStyleRes); 61 } 62 63 @Override 64 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 65 super.onLayout(changed, left, top, right, bottom); 66 invalidateOutline(); 67 } 68 69 @Override 70 public void setOutlineProvider(ViewOutlineProvider provider) { 71 mInnerOutlineProvider = provider; 72 if (mMagicalOutlineProvider == null) { 73 // Can't initialize this directly because this method is called from the superclass's 74 // constructor. 75 mMagicalOutlineProvider = new ViewOutlineProvider() { 76 @Override 77 public void getOutline(View view, Outline outline) { 78 if (getChildCount() > 0) { 79 mInnerOutlineProvider.getOutline(view, outline); 80 } else { 81 ViewOutlineProvider.BACKGROUND.getOutline(view, outline); 82 } 83 } 84 }; 85 } 86 super.setOutlineProvider(mMagicalOutlineProvider); 87 } 88} 89