• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.tv.dvr.ui;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Matrix;
22 import android.graphics.drawable.BitmapDrawable;
23 import android.transition.ChangeImageTransform;
24 import android.transition.TransitionValues;
25 import android.util.AttributeSet;
26 import android.view.View;
27 import android.widget.ImageView;
28 import android.widget.ImageView.ScaleType;
29 import com.android.tv.R;
30 import com.android.tv.ui.DetailsActivity;
31 
32 import java.util.Map;
33 
34 /**
35  * TODO: Remove this class once b/32405620 is fixed. This class is for the workaround of b/32405620
36  * and only for the shared element transition between {@link
37  * com.android.tv.dvr.ui.browse.RecordingCardView} and {@link
38  * DetailsActivity}.
39  */
40 public class ChangeImageTransformWithScaledParent extends ChangeImageTransform {
41     private static final String PROPNAME_MATRIX = "android:changeImageTransform:matrix";
42 
ChangeImageTransformWithScaledParent(Context context, AttributeSet attrs)43     public ChangeImageTransformWithScaledParent(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
47     @Override
captureStartValues(TransitionValues transitionValues)48     public void captureStartValues(TransitionValues transitionValues) {
49         super.captureStartValues(transitionValues);
50         applyParentScale(transitionValues);
51     }
52 
53     @Override
captureEndValues(TransitionValues transitionValues)54     public void captureEndValues(TransitionValues transitionValues) {
55         super.captureEndValues(transitionValues);
56         applyParentScale(transitionValues);
57     }
58 
applyParentScale(TransitionValues transitionValues)59     private void applyParentScale(TransitionValues transitionValues) {
60         View view = transitionValues.view;
61         Map<String, Object> values = transitionValues.values;
62         Matrix matrix = (Matrix) values.get(PROPNAME_MATRIX);
63         if (matrix != null
64                 && view.getId() == R.id.details_overview_image
65                 && view instanceof ImageView) {
66             ImageView imageView = (ImageView) view;
67             if (imageView.getScaleType() == ScaleType.CENTER_INSIDE
68                     && imageView.getDrawable() instanceof BitmapDrawable) {
69                 Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
70                 if (bitmap.getWidth() < imageView.getWidth()
71                         && bitmap.getHeight() < imageView.getHeight()) {
72                     float scale =
73                             imageView
74                                     .getContext()
75                                     .getResources()
76                                     .getFraction(R.fraction.lb_focus_zoom_factor_medium, 1, 1);
77                     matrix.postScale(
78                             scale, scale, imageView.getWidth() / 2, imageView.getHeight() / 2);
79                 }
80             }
81         }
82     }
83 }
84