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