ExpandableOutlineView.java revision 560e64d3d95a17123048bebe2fb06ffe4567fb78
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
38    public ExpandableOutlineView(Context context, AttributeSet attrs) {
39        super(context, attrs);
40        mRoundedRectCornerRadius = getResources().getDimensionPixelSize(
41                R.dimen.notification_material_rounded_rect_radius);
42        setOutlineProvider(new ViewOutlineProvider() {
43            @Override
44            public void getOutline(View view, Outline outline) {
45                if (!mCustomOutline) {
46                    outline.setRect(0,
47                            mClipTopAmount,
48                            getWidth(),
49                            Math.max(getActualHeight(), mClipTopAmount));
50                } else {
51                    outline.setRoundRect(mOutlineRect, mRoundedRectCornerRadius);
52                }
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 setOutlineRect(RectF rect) {
70        if (rect != null) {
71            setOutlineRect(rect.left, rect.top, rect.right, rect.bottom);
72        } else {
73            mCustomOutline = false;
74            setClipToOutline(false);
75            invalidateOutline();
76        }
77    }
78
79    protected void setOutlineRect(float left, float top, float right, float bottom) {
80        mCustomOutline = true;
81        setClipToOutline(true);
82
83        mOutlineRect.set((int) left, (int) top, (int) right, (int) bottom);
84
85        // Outlines need to be at least 1 dp
86        mOutlineRect.bottom = (int) Math.max(top, mOutlineRect.bottom);
87        mOutlineRect.right = (int) Math.max(left, mOutlineRect.right);
88
89        invalidateOutline();
90    }
91
92}
93