ExpandableOutlineView.java revision 3969ffa2f5830a680490d03fbb113a7fee900b56
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
35    public ExpandableOutlineView(Context context, AttributeSet attrs) {
36        super(context, attrs);
37        setOutlineProvider(new ViewOutlineProvider() {
38            @Override
39            public void getOutline(View view, Outline outline) {
40                if (!mCustomOutline) {
41                    outline.setRect(0,
42                            mClipTopAmount,
43                            getWidth(),
44                            Math.max(mActualHeight, mClipTopAmount));
45                } else {
46                    outline.setRect(mOutlineRect);
47                }
48            }
49        });
50    }
51
52    @Override
53    public void setActualHeight(int actualHeight, boolean notifyListeners) {
54        super.setActualHeight(actualHeight, notifyListeners);
55        invalidateOutline();
56    }
57
58    @Override
59    public void setClipTopAmount(int clipTopAmount) {
60        super.setClipTopAmount(clipTopAmount);
61        invalidateOutline();
62    }
63
64    protected void setOutlineRect(RectF rect) {
65        if (rect != null) {
66            setOutlineRect(rect.left, rect.top, rect.right, rect.bottom);
67        } else {
68            mCustomOutline = false;
69            invalidateOutline();
70        }
71    }
72
73    protected void setOutlineRect(float left, float top, float right, float bottom) {
74        mCustomOutline = true;
75
76        mOutlineRect.set((int) left, (int) top, (int) right, (int) bottom);
77
78        // Outlines need to be at least 1 dp
79        mOutlineRect.bottom = (int) Math.max(top, mOutlineRect.bottom);
80        mOutlineRect.right = (int) Math.max(left, mOutlineRect.right);
81
82        invalidateOutline();
83    }
84
85}
86