Last active
August 29, 2015 14:03
-
-
Save ohmrefresh/51110b55f7b2c974784c to your computer and use it in GitHub Desktop.
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
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
public class ScalableImageView extends ImageView { | |
public boolean isMeasured = true; | |
public ScalableImageView(Context context) { | |
super(context); | |
} | |
public ScalableImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ScalableImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
try | |
{ | |
Drawable drawable = getDrawable(); | |
if (drawable == null) | |
{ | |
setMeasuredDimension(0, 0); | |
} | |
else | |
{ | |
int width = MeasureSpec.getSize(widthMeasureSpec); | |
int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth(); | |
setMeasuredDimension(width, height); | |
} | |
} | |
catch (Exception e) | |
{ | |
isMeasured = false; | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment