MediaNowPlayingView.java revision 1dcc356c7c60f493010d69ded3d8e8f92f54217d
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.widget;
15
16import android.animation.Animator;
17import android.animation.AnimatorInflater;
18import android.animation.ObjectAnimator;
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.widget.ImageView;
24import android.widget.LinearLayout;
25import android.support.v17.leanback.R;
26
27/**
28 * The view displaying 3 animated peak meters next to each other when a media item is playing.
29 * @hide
30 */
31public class MediaNowPlayingView extends LinearLayout{
32
33    private final ImageView mImage1;
34    private final ImageView mImage2;
35    private final ImageView mImage3;
36    private final ObjectAnimator mObjectAnimator1;
37    private final ObjectAnimator mObjectAnimator2;
38    private final ObjectAnimator mObjectAnimator3;
39
40    public MediaNowPlayingView(Context context, AttributeSet attrs) {
41        super(context, attrs);
42
43        LayoutInflater.from(context).inflate(R.layout.lb_playback_now_playing_bars, this, true);
44        mImage1 = (ImageView) findViewById(R.id.bar1);
45        mImage2 = (ImageView) findViewById(R.id.bar2);
46        mImage3 = (ImageView) findViewById(R.id.bar3);
47
48        mImage1.setPivotY(mImage1.getDrawable().getIntrinsicHeight());
49        mImage2.setPivotY(mImage2.getDrawable().getIntrinsicHeight());
50        mImage3.setPivotY(mImage3.getDrawable().getIntrinsicHeight());
51
52        setDropScale(mImage1);
53        setDropScale(mImage2);
54        setDropScale(mImage3);
55
56        mObjectAnimator1 = (ObjectAnimator) AnimatorInflater.loadAnimator(context,
57                R.animator.lb_playback_now_bar1_animator);
58        mObjectAnimator1.setTarget(mImage1);
59
60        mObjectAnimator2 = (ObjectAnimator) AnimatorInflater.loadAnimator(context,
61                R.animator.lb_playback_now_bar2_animator);
62        mObjectAnimator2.setTarget(mImage2);
63
64        mObjectAnimator3 = (ObjectAnimator) AnimatorInflater.loadAnimator(context,
65                R.animator.lb_playback_now_bar3_animator);
66        mObjectAnimator3.setTarget(mImage3);
67    }
68
69    static void setDropScale(View view) {
70        view.setScaleY(1f / 12f);
71    }
72
73    @Override
74    public void setVisibility(int visibility) {
75        super.setVisibility(visibility);
76        if (visibility == View.GONE) {
77            stopAnimation();
78        } else {
79            startAnimation();
80        }
81    }
82
83    @Override
84    protected void onAttachedToWindow() {
85        super.onAttachedToWindow();
86        if (getVisibility() == View.VISIBLE)
87            startAnimation();
88    }
89
90    @Override
91    protected void onDetachedFromWindow() {
92        super.onDetachedFromWindow();
93        stopAnimation();
94    }
95
96    private void startAnimation() {
97        startAnimation(mObjectAnimator1);
98        startAnimation(mObjectAnimator2);
99        startAnimation(mObjectAnimator3);
100        mImage1.setVisibility(View.VISIBLE);
101        mImage2.setVisibility(View.VISIBLE);
102        mImage3.setVisibility(View.VISIBLE);
103    }
104
105    private void stopAnimation() {
106        stopAnimation(mObjectAnimator1, mImage1);
107        stopAnimation(mObjectAnimator2, mImage2);
108        stopAnimation(mObjectAnimator3, mImage3);
109        mImage1.setVisibility(View.GONE);
110        mImage2.setVisibility(View.GONE);
111        mImage3.setVisibility(View.GONE);
112    }
113
114    private void startAnimation(Animator animator) {
115        if (!animator.isStarted()) {
116            animator.start();
117        }
118    }
119
120    private void stopAnimation(Animator animator, View view) {
121        if (animator.isStarted()) {
122            animator.cancel();
123            setDropScale(view);
124        }
125    }
126}
127