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