Created
July 24, 2012 10:20
-
-
Save menny/3169277 to your computer and use it in GitHub Desktop.
Android's ImageView scaling hack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.evendanan.android.hacks; | |
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
/**** | |
*Note, this ImageView hack will ALWAYS scale the image (up or down) to fit the available | |
*space, while keeping the image aspect ratio. | |
*/ | |
public class ScalingImageView extends android.widget.ImageView { | |
private static final String TAG = "ScalingImageView"; | |
public ScalingImageView(Context context) { | |
super(context); | |
} | |
public ScalingImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ScalingImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
Log.d(TAG, "onMeasure w:"+widthMeasureSpec+" h:"+heightMeasureSpec); | |
Log.d(TAG, "onMeasure result w:"+getMeasuredWidth()+" h:"+getMeasuredHeight()); | |
Drawable d = getDrawable(); | |
if (d != null) { | |
Log.d(TAG, "Drawable w:"+d.getMinimumWidth()+" h:"+d.getMinimumHeight()); | |
//let's try to fix things up. | |
final double ratio = ((double)d.getMinimumWidth())/((double)d.getMinimumHeight()); | |
//getting the minimum dim of this view | |
int idealWidth = (int)(getMeasuredHeight() * ratio); | |
int idealHeight = (int)(getMeasuredWidth() / ratio); | |
//these are ideal, but with no respect to the other dim | |
Log.d(TAG, "Ideal dims w:"+idealWidth+" h:"+idealHeight); | |
//now.. Who is breaking the borders of the view? | |
final int width; | |
final int height; | |
if (idealWidth > getMeasuredWidth()) { | |
//I should reduce the height size. | |
width = getMeasuredWidth(); | |
height = idealHeight; | |
} else { | |
width = idealWidth; | |
height = getMeasuredHeight(); | |
} | |
Log.d(TAG, "onMeasure new values w:"+width+" h:"+height); | |
setMeasuredDimension(width, height); | |
} else { | |
Log.d(TAG, "Drawable is null"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment