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