Last active
August 29, 2015 14:01
-
-
Save ataulm/0f359f8ae836908dca50 to your computer and use it in GitHub Desktop.
look Antonio, no hands!
This file contains 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.ataulm.mijur.view; | |
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
public class MatchParentWidthImageView extends ImageView { | |
public MatchParentWidthImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public MatchParentWidthImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
Drawable drawable = getDrawable(); | |
if (drawable == null) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} else { | |
int desiredHeight = calculateDesiredHeight(drawable, widthMeasureSpec); | |
int desiredHeightMeasureSpec = MeasureSpec.makeMeasureSpec(desiredHeight, MeasureSpec.EXACTLY); | |
super.onMeasure(widthMeasureSpec, desiredHeightMeasureSpec); | |
} | |
} | |
private int calculateDesiredHeight(Drawable drawable, int widthMeasureSpec) { | |
float width = drawable.getIntrinsicWidth(); | |
float height = drawable.getIntrinsicHeight(); | |
float aspectRatio = width / height; | |
int availableWidth = MeasureSpec.getSize(widthMeasureSpec); | |
return (int) (availableWidth / aspectRatio + 0.5f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment