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