ExpandableOutlineView.java revision 924c612b87ff657d189979ccd219bd445d14c97c
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
27import com.android.systemui.R;
28
29/**
30 * Like {@link ExpandableView}, but setting an outline for the height and clipping.
31 */
32public abstract class ExpandableOutlineView extends ExpandableView {
33
34    private final Rect mOutlineRect = new Rect();
35    protected final int mRoundedRectCornerRadius;
36    private boolean mCustomOutline;
37    private float mOutlineAlpha = 1f;
38
39    public ExpandableOutlineView(Context context, AttributeSet attrs) {
40        super(context, attrs);
41        setOutlineProvider(new ViewOutlineProvider() {
42            @Override
43            public void getOutline(View view, Outline outline) {
44                if (!mCustomOutline) {
45                    outline.setRect(0,
46                            mClipTopAmount,
47                            getWidth(),
48                            Math.max(getActualHeight(), mClipTopAmount));
49                } else {
50                    outline.setRoundRect(mOutlineRect, mRoundedRectCornerRadius);
51                }
52                outline.setAlpha(mOutlineAlpha);
53            }
54        });
55    }
56
57    @Override
58    public void setActualHeight(int actualHeight, boolean notifyListeners) {
59        super.setActualHeight(actualHeight, notifyListeners);
60        invalidateOutline();
61    }
62
63    @Override
64    public void setClipTopAmount(int clipTopAmount) {
65        super.setClipTopAmount(clipTopAmount);
66        invalidateOutline();
67    }
68
69    protected void setOutlineAlpha(float alpha) {
70        mOutlineAlpha = alpha;
71        invalidateOutline();
72    }
73
74    protected void setOutlineRect(RectF rect) {
75        if (rect != null) {
76            setOutlineRect(rect.left, rect.top, rect.right, rect.bottom);
77        } else {
78            mCustomOutline = false;
79            setClipToOutline(false);
80            invalidateOutline();
81        }
82    }
83
84    protected void setOutlineRect(float left, float top, float right, float bottom) {
85        mCustomOutline = true;
86        setClipToOutline(true);
87
88        mOutlineRect.set((int) left, (int) top, (int) right, (int) bottom);
89
90        // Outlines need to be at least 1 dp
91        mOutlineRect.bottom = (int) Math.max(top, mOutlineRect.bottom);
92        mOutlineRect.right = (int) Math.max(left, mOutlineRect.right);
93
94        invalidateOutline();
95    }
96
97}
98