Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org.mozilla.gecko.widget;
8 import android.content.Context;
9 import android.graphics.Canvas;
10 import android.graphics.Matrix;
11 import android.graphics.drawable.Drawable;
12 import android.util.AttributeSet;
13 import android.widget.ImageView;
15 /* Special version of ImageView for thumbnails. Scales a thumbnail so that it maintains its aspect
16 * ratio and so that the images width and height are the same size or greater than the view size
17 */
18 public class ThumbnailView extends ImageView {
19 private static final String LOGTAG = "GeckoThumbnailView";
20 final private Matrix mMatrix;
21 private int mWidthSpec = -1;
22 private int mHeightSpec = -1;
23 private boolean mLayoutChanged;
25 public ThumbnailView(Context context, AttributeSet attrs) {
26 super(context, attrs);
27 mMatrix = new Matrix();
28 mLayoutChanged = true;
29 }
31 @Override
32 public void onDraw(Canvas canvas) {
33 Drawable d = getDrawable();
34 if (mLayoutChanged) {
35 int w1 = d.getIntrinsicWidth();
36 int h1 = d.getIntrinsicHeight();
37 int w2 = getWidth();
38 int h2 = getHeight();
40 float scale = (w2/h2 < w1/h1) ? (float)h2/h1 : (float)w2/w1;
41 mMatrix.setScale(scale, scale);
42 }
44 int saveCount = canvas.save();
45 canvas.concat(mMatrix);
46 d.draw(canvas);
47 canvas.restoreToCount(saveCount);
48 }
50 @Override
51 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
52 // OnLayout.changed isn't a reliable measure of whether or not the size of this view has changed
53 // neither is onSizeChanged called often enough. Instead, we track changes in size ourselves, and
54 // only invalidate this matrix if we have a new width/height spec
55 if (widthMeasureSpec != mWidthSpec || heightMeasureSpec != mHeightSpec) {
56 mWidthSpec = widthMeasureSpec;
57 mHeightSpec = heightMeasureSpec;
58 mLayoutChanged = true;
59 }
60 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
61 }
62 }