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