MediaNowPlayingView.java revision c39d9c75590eca86a5e7e32a8824ba04a0d42e9b
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.support.annotation.RestrictTo; 21import android.util.AttributeSet; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.widget.ImageView; 25import android.widget.LinearLayout; 26import android.support.v17.leanback.R; 27 28import static android.support.annotation.RestrictTo.Scope.GROUP_ID; 29 30/** 31 * The view displaying 3 animated peak meters next to each other when a media item is playing. 32 * @hide 33 */ 34@RestrictTo(GROUP_ID) 35public class MediaNowPlayingView extends LinearLayout{ 36 37 private final ImageView mImage1; 38 private final ImageView mImage2; 39 private final ImageView mImage3; 40 private final ObjectAnimator mObjectAnimator1; 41 private final ObjectAnimator mObjectAnimator2; 42 private final ObjectAnimator mObjectAnimator3; 43 44 public MediaNowPlayingView(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 47 LayoutInflater.from(context).inflate(R.layout.lb_playback_now_playing_bars, this, true); 48 mImage1 = (ImageView) findViewById(R.id.bar1); 49 mImage2 = (ImageView) findViewById(R.id.bar2); 50 mImage3 = (ImageView) findViewById(R.id.bar3); 51 52 mImage1.setPivotY(mImage1.getDrawable().getIntrinsicHeight()); 53 mImage2.setPivotY(mImage2.getDrawable().getIntrinsicHeight()); 54 mImage3.setPivotY(mImage3.getDrawable().getIntrinsicHeight()); 55 56 setDropScale(mImage1); 57 setDropScale(mImage2); 58 setDropScale(mImage3); 59 60 mObjectAnimator1 = (ObjectAnimator) AnimatorInflater.loadAnimator(context, 61 R.animator.lb_playback_now_bar1_animator); 62 mObjectAnimator1.setTarget(mImage1); 63 64 mObjectAnimator2 = (ObjectAnimator) AnimatorInflater.loadAnimator(context, 65 R.animator.lb_playback_now_bar2_animator); 66 mObjectAnimator2.setTarget(mImage2); 67 68 mObjectAnimator3 = (ObjectAnimator) AnimatorInflater.loadAnimator(context, 69 R.animator.lb_playback_now_bar3_animator); 70 mObjectAnimator3.setTarget(mImage3); 71 } 72 73 static void setDropScale(View view) { 74 view.setScaleY(1f / 12f); 75 } 76 77 @Override 78 public void setVisibility(int visibility) { 79 super.setVisibility(visibility); 80 if (visibility == View.GONE) { 81 stopAnimation(); 82 } else { 83 startAnimation(); 84 } 85 } 86 87 @Override 88 protected void onAttachedToWindow() { 89 super.onAttachedToWindow(); 90 if (getVisibility() == View.VISIBLE) 91 startAnimation(); 92 } 93 94 @Override 95 protected void onDetachedFromWindow() { 96 super.onDetachedFromWindow(); 97 stopAnimation(); 98 } 99 100 private void startAnimation() { 101 startAnimation(mObjectAnimator1); 102 startAnimation(mObjectAnimator2); 103 startAnimation(mObjectAnimator3); 104 mImage1.setVisibility(View.VISIBLE); 105 mImage2.setVisibility(View.VISIBLE); 106 mImage3.setVisibility(View.VISIBLE); 107 } 108 109 private void stopAnimation() { 110 stopAnimation(mObjectAnimator1, mImage1); 111 stopAnimation(mObjectAnimator2, mImage2); 112 stopAnimation(mObjectAnimator3, mImage3); 113 mImage1.setVisibility(View.GONE); 114 mImage2.setVisibility(View.GONE); 115 mImage3.setVisibility(View.GONE); 116 } 117 118 private void startAnimation(Animator animator) { 119 if (!animator.isStarted()) { 120 animator.start(); 121 } 122 } 123 124 private void stopAnimation(Animator animator, View view) { 125 if (animator.isStarted()) { 126 animator.cancel(); 127 setDropScale(view); 128 } 129 } 130} 131