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