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 17 package com.android.systemui.statusbar.policy; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.Color; 22 import android.graphics.ColorFilter; 23 import android.graphics.Paint; 24 import android.graphics.PixelFormat; 25 import android.graphics.RadialGradient; 26 import android.graphics.Rect; 27 import android.graphics.Shader; 28 import android.graphics.drawable.Drawable; 29 import android.view.View; 30 31 import com.android.systemui.R; 32 33 /** 34 * Gradient background for the user switcher on Keyguard. 35 */ 36 public 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.25f; 41 42 private int mDarkColor; 43 private int mAlpha = 255; 44 private Paint mRadialGradientPaint = new Paint(); 45 private int mCircleX; 46 private int mCircleY; 47 private int mSize; 48 KeyguardUserSwitcherScrim(Context context)49 public KeyguardUserSwitcherScrim(Context context) { 50 mDarkColor = context.getColor( 51 R.color.keyguard_user_switcher_background_gradient_color); 52 } 53 54 @Override draw(Canvas canvas)55 public void draw(Canvas canvas) { 56 if (mAlpha == 0) { 57 return; 58 } 59 Rect bounds = getBounds(); 60 canvas.drawRect(bounds.left, bounds.top, bounds.right, bounds.bottom, mRadialGradientPaint); 61 } 62 63 @Override setAlpha(int alpha)64 public void setAlpha(int alpha) { 65 mAlpha = alpha; 66 updatePaint(); 67 invalidateSelf(); 68 } 69 70 @Override getAlpha()71 public int getAlpha() { 72 return mAlpha; 73 } 74 75 @Override setColorFilter(ColorFilter colorFilter)76 public void setColorFilter(ColorFilter colorFilter) { 77 } 78 79 @Override getOpacity()80 public int getOpacity() { 81 return PixelFormat.TRANSLUCENT; 82 } 83 84 @Override onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)85 public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, 86 int oldTop, int oldRight, int oldBottom) { 87 if (left != oldLeft || top != oldTop || right != oldRight || bottom != oldBottom) { 88 int width = right - left; 89 int height = bottom - top; 90 mSize = Math.max(width, height); 91 updatePaint(); 92 } 93 } 94 updatePaint()95 private void updatePaint() { 96 if (mSize == 0) { 97 return; 98 } 99 float outerRadius = mSize * OUTER_EXTENT; 100 mRadialGradientPaint.setShader( 101 new RadialGradient(mCircleX, mCircleY, outerRadius, 102 new int[] { Color.argb( 103 (int) (Color.alpha(mDarkColor) * mAlpha / 255f), 0, 0, 0), 104 Color.TRANSPARENT }, 105 new float[] { Math.max(0f, INNER_EXTENT / OUTER_EXTENT), 1f }, 106 Shader.TileMode.CLAMP)); 107 } 108 109 /** 110 * Sets the center of the radial gradient used as a background 111 * 112 * @param x 113 * @param y 114 */ setGradientCenter(int x, int y)115 public void setGradientCenter(int x, int y) { 116 mCircleX = x; 117 mCircleY = y; 118 updatePaint(); 119 } 120 } 121