KeyguardUserSwitcherScrim.java revision bf1899e64c0fbb0fa211d883134012299fe79f41
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.policy;
18
19import android.graphics.Canvas;
20import android.graphics.Color;
21import android.graphics.ColorFilter;
22import android.graphics.Paint;
23import android.graphics.PixelFormat;
24import android.graphics.RadialGradient;
25import android.graphics.Rect;
26import android.graphics.Shader;
27import android.graphics.drawable.Drawable;
28import android.util.LayoutDirection;
29import android.view.View;
30
31import com.android.systemui.R;
32
33/**
34 * Gradient background for the user switcher on Keyguard.
35 */
36public class KeyguardUserSwitcherScrim extends Drawable
37        implements View.OnLayoutChangeListener {
38
39    private static final float OUTER_EXTENT = 2.5f;
40    private static final float INNER_EXTENT = 0.75f;
41
42    private int mDarkColor;
43    private int mTop;
44    private Paint mRadialGradientPaint = new Paint();
45
46    public KeyguardUserSwitcherScrim(View host) {
47        host.addOnLayoutChangeListener(this);
48        mDarkColor = host.getResources().getColor(
49                R.color.keyguard_user_switcher_background_gradient_color);
50    }
51
52    @Override
53    public void draw(Canvas canvas) {
54        boolean isLtr = getLayoutDirection() == LayoutDirection.LTR;
55        Rect bounds = getBounds();
56        float width = bounds.width() * OUTER_EXTENT;
57        float height = (mTop + bounds.height()) * OUTER_EXTENT;
58        canvas.translate(0, -mTop);
59        canvas.scale(1, height/width);
60        canvas.drawRect(isLtr ? bounds.right - width : 0, 0,
61                isLtr ? bounds.right : bounds.left + width, width, mRadialGradientPaint);
62    }
63
64    @Override
65    public void setAlpha(int alpha) {
66    }
67
68    @Override
69    public void setColorFilter(ColorFilter cf) {
70    }
71
72    @Override
73    public int getOpacity() {
74        return PixelFormat.TRANSLUCENT;
75    }
76
77    @Override
78    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
79            int oldTop, int oldRight, int oldBottom) {
80        if (left != oldLeft || top != oldTop || right != oldRight || bottom != oldBottom) {
81            int width = right - left;
82            float radius = width * OUTER_EXTENT;
83            boolean isLtr = getLayoutDirection() == LayoutDirection.LTR;
84            mRadialGradientPaint.setShader(
85                    new RadialGradient(isLtr ? width : 0, 0, radius,
86                            new int[] { mDarkColor, Color.TRANSPARENT},
87                            new float[] { Math.max(0f, width * INNER_EXTENT / radius), 1f},
88                            Shader.TileMode.CLAMP));
89            mTop = top;
90        }
91    }
92}
93