Last active
August 29, 2015 13:55
-
-
Save joshmcarthur/8704584 to your computer and use it in GitHub Desktop.
An Android widget to fit an ImageView to the screen bounds of a device without affecting it's aspect ratio.
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 com.company.sample.widgets; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
/** | |
* FittedImageView | |
* @extends ImageView | |
* | |
* Provides a display for an image in such as way that it is able | |
* to stretch to fill the width of the screen without stretching the image | |
* out of aspect ratio. | |
* | |
* @author joshmcarthur | |
* | |
*/ | |
public class FittedImageView extends ImageView { | |
/** | |
* Constructor. | |
* | |
* @param context (required) the context to pass to the superclass ImageView | |
*/ | |
public FittedImageView(Context context) { | |
super(context); | |
} | |
/** | |
* Constructor. | |
* | |
* @param context (required) the context to pass to the superclass ImageView | |
* @param attrs (optional) the attributes to pass to the superclass ImageView | |
*/ | |
public FittedImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
/** | |
* Constructor. | |
* | |
* @param context (required) the context to pass to the superclass ImageView | |
* @param attrs (optional) the attributes to pass to the superclass ImageView | |
* @param defStyle (optional) the style ID to pass to the superclass ImageView | |
*/ | |
public FittedImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
/** | |
* Override of onMeasure to resize the drawable to retain it's aspect ratio. | |
* | |
* @param widthMeasureSpec (required) the width measure to derive size from | |
* @param heightMeasureSpec (required) the height measure required by superclass | |
* method definition. | |
*/ | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int width = MeasureSpec.getSize(widthMeasureSpec); | |
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth(); | |
setMeasuredDimension(width, height); | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<com.company.sample.widgets.FittedImageView | |
android:id="@+id/capturedImageView" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:adjustViewBounds="true" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true" /> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment