blob: 32c26ba8ba133bcca26c7ace0a95c65356150a10 [file] [log] [blame]
Selim Cinek33223572016-02-19 19:32:22 -08001/*
2 * Copyright (C) 2016 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.notification;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.graphics.Outline;
22import android.util.AttributeSet;
23import android.view.View;
24import android.view.ViewGroup;
25import android.view.ViewOutlineProvider;
26import android.widget.LinearLayout;
27
28import com.android.systemui.statusbar.AlphaOptimizedFrameLayout;
29
30/**
31 * A view used to cast a shadow of a certain size on another view
32 */
33public class FakeShadowView extends AlphaOptimizedFrameLayout {
34 public static final float SHADOW_SIBLING_TRESHOLD = 0.1f;
35
36 private View mFakeShadow;
37 private float mOutlineAlpha;
38
39 public FakeShadowView(Context context) {
40 this(context, null);
41 }
42
43 public FakeShadowView(Context context, @Nullable AttributeSet attrs) {
44 this(context, attrs, 0);
45 }
46
47 public FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
48 this(context, attrs, defStyleAttr, 0);
49 }
50
51 public FakeShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
52 int defStyleRes) {
53 super(context, attrs, defStyleAttr, defStyleRes);
54 mFakeShadow = new View(context);
55 mFakeShadow.setVisibility(INVISIBLE);
56 mFakeShadow.setLayoutParams(new LinearLayout.LayoutParams(
57 ViewGroup.LayoutParams.MATCH_PARENT,
58 (int) (48 * getResources().getDisplayMetrics().density)));
59 mFakeShadow.setOutlineProvider(new ViewOutlineProvider() {
60 @Override
61 public void getOutline(View view, Outline outline) {
62 outline.setRect(0, 0, getWidth(), mFakeShadow.getHeight());
63 outline.setAlpha(mOutlineAlpha);
64 }
65 });
66 addView(mFakeShadow);
67 }
68
69 public void setFakeShadowTranslationZ(float fakeShadowTranslationZ, float outlineAlpha,
70 int shadowYEnd, int outlineTranslation) {
71 if (fakeShadowTranslationZ == 0.0f) {
72 mFakeShadow.setVisibility(INVISIBLE);
73 } else {
74 mFakeShadow.setVisibility(VISIBLE);
75 mFakeShadow.setTranslationZ(fakeShadowTranslationZ);
76 mFakeShadow.setTranslationX(outlineTranslation);
77 mFakeShadow.setTranslationY(shadowYEnd - mFakeShadow.getHeight());
78 if (outlineAlpha != mOutlineAlpha) {
79 mOutlineAlpha = outlineAlpha;
80 mFakeShadow.invalidateOutline();
81 }
82 }
83 }
84}