blob: 21ab64dab9dfeadbbda631beca98e1b7e2db9746 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
2 * Copyright (C) 2017 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 */
16package com.android.wallpaper.widget;
17
18import android.content.Context;
19import android.graphics.Matrix;
20import android.support.annotation.Nullable;
21import android.util.AttributeSet;
22import android.widget.ImageView;
23
24/**
25 * Custom ImageView that mimics the home launcher screen wallpaper position by aligning start and
26 * centering vertically its drawable. Scales down the image as much as possible without
27 * letterboxing.
28 */
29public class WallpaperThumbnailView extends ImageView {
30
31 public WallpaperThumbnailView(Context context) {
32 super(context);
33 setScaleType(ScaleType.MATRIX);
34 }
35
36 public WallpaperThumbnailView(Context context, @Nullable AttributeSet attrs) {
37 super(context, attrs);
38 setScaleType(ScaleType.MATRIX);
39 }
40
41 @Override
42 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
43 super.onLayout(changed, left, top, right, bottom);
44 recomputeImageMatrix();
45 }
46
47 private void recomputeImageMatrix() {
48 // The drawable may be null on the first layout pass.
49 if (getDrawable() == null) {
50 return;
51 }
52
53 final Matrix matrix = getImageMatrix();
54
55 final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
56 final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
57 final int drawableWidth = getDrawable().getIntrinsicWidth();
58 final int drawableHeight = getDrawable().getIntrinsicHeight();
59
60 float scale;
61 if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
62 scale = (float) viewHeight / drawableHeight;
63 } else {
64 scale = (float) viewWidth / drawableWidth;
65 }
66
67 // Set scale such that the maximum area of the drawable is shown without letterboxing.
68 matrix.setScale(scale, scale);
69
70 // Center the drawable vertically within the view.
71 if ((drawableHeight * scale) > viewHeight) {
72 float dy = -(((drawableHeight * scale) - viewHeight) / 2f);
73 matrix.postTranslate(0, dy);
74 }
75
76 // If layout direction is RTL, then matrix should align the image towards the right.
77 if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
78 float dx = -((drawableWidth * scale) - viewWidth);
79 matrix.postTranslate(dx, 0);
80 }
81
82 setImageMatrix(matrix);
83 invalidate();
84 }
85}