您的位置:首页 > 其它

SmartImageView

2016-01-21 22:30 274 查看
==

public class SmartImageView extends ImageView {
public SmartImageView(Context context)
{
super(context);
}

public SmartImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}

public SmartImageView(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 {
float imageRatio = (float)drawable.getIntrinsicWidth() / drawable.getIntrinsicHeight();
float viewRatio = (float)MeasureSpec.getSize(widthMeasureSpec) / MeasureSpec.getSize(heightMeasureSpec);

// Image is wider than the display (ratio)
if (imageRatio >= viewRatio) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width / imageRatio);
setMeasuredDimension(width, height);
}
// Image is taller than the display (ratio)
else {
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = (int)(height * imageRatio);
setMeasuredDimension(width, height);
}
}
} catch (Exception e) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}


==
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: