ExpandableOutlineView.java revision 7b7ca3cdf25bee73aa9e58aaaf6ce75fe81bd045
1/* 2 * Copyright (C) 2014 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 com.android.systemui.statusbar; 18 19import android.content.Context; 20import android.graphics.Outline; 21import android.graphics.Rect; 22import android.graphics.RectF; 23import android.util.AttributeSet; 24import android.view.View; 25import android.view.ViewOutlineProvider; 26 27/** 28 * Like {@link ExpandableView}, but setting an outline for the height and clipping. 29 */ 30public abstract class ExpandableOutlineView extends ExpandableView { 31 32 private final Rect mOutlineRect = new Rect(); 33 private boolean mCustomOutline; 34 private float mDensity; 35 36 public ExpandableOutlineView(Context context, AttributeSet attrs) { 37 super(context, attrs); 38 mDensity = getResources().getDisplayMetrics().density; 39 setOutlineProvider(new ViewOutlineProvider() { 40 @Override 41 public boolean getOutline(View view, Outline outline) { 42 if (!mCustomOutline) { 43 outline.setRect(0, 44 mClipTopAmount, 45 getWidth(), 46 Math.max(mActualHeight, mClipTopAmount)); 47 } else { 48 outline.setRect(mOutlineRect); 49 } 50 return true; 51 } 52 }); 53 } 54 55 @Override 56 public void setActualHeight(int actualHeight, boolean notifyListeners) { 57 super.setActualHeight(actualHeight, notifyListeners); 58 invalidateOutline(); 59 } 60 61 @Override 62 public void setClipTopAmount(int clipTopAmount) { 63 super.setClipTopAmount(clipTopAmount); 64 invalidateOutline(); 65 } 66 67 protected void setOutlineRect(RectF rect) { 68 if (rect != null) { 69 setOutlineRect(rect.left, rect.top, rect.right, rect.bottom); 70 } else { 71 mCustomOutline = false; 72 invalidateOutline(); 73 } 74 } 75 76 protected void setOutlineRect(float left, float top, float right, float bottom) { 77 mCustomOutline = true; 78 79 mOutlineRect.set((int) left, (int) top, (int) right, (int) bottom); 80 81 // Outlines need to be at least 1 dp 82 mOutlineRect.bottom = (int) Math.max(top + mDensity, mOutlineRect.bottom); 83 mOutlineRect.right = (int) Math.max(left + mDensity, mOutlineRect.right); 84 85 invalidateOutline(); 86 } 87 88} 89