1 /* 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 package com.android.car.media; 17 18 import android.content.Context; 19 import android.graphics.Bitmap; 20 import android.graphics.Color; 21 import android.graphics.ColorFilter; 22 import android.graphics.ColorMatrix; 23 import android.graphics.ColorMatrixColorFilter; 24 import android.support.car.ui.AnimationListenerAdapter; 25 import android.support.car.ui.LogDecelerateInterpolator; 26 import android.util.AttributeSet; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.view.animation.Animation; 30 import android.view.animation.AnimationUtils; 31 import android.widget.FrameLayout; 32 import android.widget.ImageView; 33 34 /** 35 * A view where updating the image will show certain animations. Current animations include fading 36 * in and scaling down the new image. 37 */ 38 public class CrossfadeImageView extends FrameLayout { 39 // ColorFilters can't currently be modified (b/17262092) so creating a saturation fade with 40 // color filters would normally require creating a ton of small objects. We get around this by 41 // caching color filters and limit the saturation to increments of 0.1. 42 // 0-0.09 -> [0] 43 // 0.10-0.19 -> [1] 44 // ... 45 // 1.0 -> [10] 46 private final ColorFilter[] mSaturationColorFilters = new ColorFilter[11]; 47 private final ColorMatrix mColorMatrix = new ColorMatrix(); 48 private final ImageView mImageView1; 49 private final ImageView mImageView2; 50 51 private ImageView mActiveImageView; 52 private ImageView mInactiveImageView; 53 54 private Bitmap mCurrentBitmap = null; 55 private Integer mCurrentColor = null; 56 private Animation mImageInAnimation; 57 private Animation mImageOutAnimation; 58 59 public CrossfadeImageView(Context context) { 60 this(context, null); 61 } 62 63 public CrossfadeImageView(Context context, AttributeSet attrs) { 64 this(context, attrs, 0); 65 } 66 67 public CrossfadeImageView(Context context, AttributeSet attrs, int defStyleAttr) { 68 this(context, attrs, defStyleAttr, 0); 69 } 70 71 public CrossfadeImageView(Context context, AttributeSet attrs, 72 int defStyleAttr, int defStyleRes) { 73 super(context, attrs, defStyleAttr, defStyleRes); 74 LayoutParams lp = new LayoutParams( 75 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 76 ImageView imageViewBackground = new ImageView(context, attrs, defStyleAttr, defStyleRes); 77 imageViewBackground.setLayoutParams(lp); 78 imageViewBackground.setBackgroundColor(Color.BLACK); 79 addView(imageViewBackground); 80 mImageView1 = new ImageView(context, attrs, defStyleAttr, defStyleRes); 81 mImageView1.setLayoutParams(lp); 82 addView(mImageView1); 83 mImageView2 = new ImageView(context, attrs, defStyleAttr, defStyleRes); 84 mImageView2.setLayoutParams(lp); 85 addView(mImageView2); 86 87 mActiveImageView = mImageView1; 88 mInactiveImageView = mImageView2; 89 90 mImageInAnimation = AnimationUtils.loadAnimation(context, R.anim.image_in); 91 mImageInAnimation.setInterpolator(new LogDecelerateInterpolator(100, 0)); 92 mImageOutAnimation = AnimationUtils.loadAnimation(context, R.anim.image_out); 93 } 94 95 public void setImageBitmap(Bitmap bitmap, boolean showAnimation) { 96 if (bitmap != null && bitmap.sameAs(mCurrentBitmap)) { 97 return; 98 } 99 mCurrentBitmap = bitmap; 100 mCurrentColor = null; 101 mInactiveImageView.setImageBitmap(bitmap); 102 if (showAnimation) { 103 animateViews(); 104 } else { 105 mActiveImageView.setImageBitmap(bitmap); 106 } 107 } 108 109 @Override 110 public void setBackgroundColor(int color) { 111 if (mCurrentColor != null && mCurrentColor == color) { 112 return; 113 } 114 mInactiveImageView.setImageBitmap(null); 115 mCurrentBitmap = null; 116 mCurrentColor = color; 117 mInactiveImageView.setBackgroundColor(color); 118 animateViews(); 119 } 120 121 public void setSaturation(float saturation) { 122 int i = (int) ((saturation * 100) / 10); 123 ColorFilter cf = mSaturationColorFilters[i]; 124 if (cf == null) { 125 mColorMatrix.setSaturation((10 * i) / 100f); 126 cf = new ColorMatrixColorFilter(mColorMatrix); 127 mSaturationColorFilters[i] = cf; 128 } 129 130 mImageView1.setColorFilter(cf); 131 mImageView2.setColorFilter(cf); 132 } 133 134 private final Animation.AnimationListener mAnimationListener = new AnimationListenerAdapter() { 135 @Override 136 public void onAnimationEnd(Animation animation) { 137 if (mInactiveImageView != null) { 138 mInactiveImageView.setVisibility(View.GONE); 139 } 140 } 141 }; 142 143 private void animateViews() { 144 mInactiveImageView.setVisibility(View.VISIBLE); 145 mInactiveImageView.startAnimation(mImageInAnimation); 146 mInactiveImageView.bringToFront(); 147 mActiveImageView.startAnimation(mImageOutAnimation); 148 mImageOutAnimation.setAnimationListener(mAnimationListener); 149 if (mActiveImageView == mImageView1) { 150 mActiveImageView = mImageView2; 151 mInactiveImageView = mImageView1; 152 } else { 153 mActiveImageView = mImageView1; 154 mInactiveImageView = mImageView2; 155 } 156 } 157 } 158