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.internal; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.Canvas; 22import android.graphics.Rect; 23import android.graphics.drawable.Drawable; 24import android.support.annotation.NonNull; 25import android.support.design.R; 26import android.support.v4.view.ViewCompat; 27import android.support.v4.view.WindowInsetsCompat; 28import android.util.AttributeSet; 29import android.view.View; 30import android.widget.FrameLayout; 31 32/** 33 * @hide 34 */ 35public class ScrimInsetsFrameLayout extends FrameLayout { 36 37 private Drawable mInsetForeground; 38 39 private Rect mInsets; 40 41 private Rect mTempRect = new Rect(); 42 43 public ScrimInsetsFrameLayout(Context context) { 44 this(context, null); 45 } 46 47 public ScrimInsetsFrameLayout(Context context, AttributeSet attrs) { 48 this(context, attrs, 0); 49 } 50 51 public ScrimInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { 52 super(context, attrs, defStyleAttr); 53 54 final TypedArray a = context.obtainStyledAttributes(attrs, 55 R.styleable.ScrimInsetsFrameLayout, defStyleAttr, 56 R.style.Widget_Design_ScrimInsetsFrameLayout); 57 mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsFrameLayout_insetForeground); 58 a.recycle(); 59 setWillNotDraw(true); // No need to draw until the insets are adjusted 60 61 ViewCompat.setOnApplyWindowInsetsListener(this, 62 new android.support.v4.view.OnApplyWindowInsetsListener() { 63 @Override 64 public WindowInsetsCompat onApplyWindowInsets(View v, 65 WindowInsetsCompat insets) { 66 if (null == mInsets) { 67 mInsets = new Rect(); 68 } 69 mInsets.set(insets.getSystemWindowInsetLeft(), 70 insets.getSystemWindowInsetTop(), 71 insets.getSystemWindowInsetRight(), 72 insets.getSystemWindowInsetBottom()); 73 onInsetsChanged(mInsets); 74 setWillNotDraw(mInsets.isEmpty() || mInsetForeground == null); 75 ViewCompat.postInvalidateOnAnimation(ScrimInsetsFrameLayout.this); 76 return insets.consumeSystemWindowInsets(); 77 } 78 }); 79 } 80 81 @Override 82 public void draw(@NonNull Canvas canvas) { 83 super.draw(canvas); 84 85 int width = getWidth(); 86 int height = getHeight(); 87 if (mInsets != null && mInsetForeground != null) { 88 int sc = canvas.save(); 89 canvas.translate(getScrollX(), getScrollY()); 90 91 // Top 92 mTempRect.set(0, 0, width, mInsets.top); 93 mInsetForeground.setBounds(mTempRect); 94 mInsetForeground.draw(canvas); 95 96 // Bottom 97 mTempRect.set(0, height - mInsets.bottom, width, height); 98 mInsetForeground.setBounds(mTempRect); 99 mInsetForeground.draw(canvas); 100 101 // Left 102 mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom); 103 mInsetForeground.setBounds(mTempRect); 104 mInsetForeground.draw(canvas); 105 106 // Right 107 mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom); 108 mInsetForeground.setBounds(mTempRect); 109 mInsetForeground.draw(canvas); 110 111 canvas.restoreToCount(sc); 112 } 113 } 114 115 @Override 116 protected void onAttachedToWindow() { 117 super.onAttachedToWindow(); 118 if (mInsetForeground != null) { 119 mInsetForeground.setCallback(this); 120 } 121 } 122 123 @Override 124 protected void onDetachedFromWindow() { 125 super.onDetachedFromWindow(); 126 if (mInsetForeground != null) { 127 mInsetForeground.setCallback(null); 128 } 129 } 130 131 protected void onInsetsChanged(Rect insets) { 132 } 133 134} 135