码迷,mamicode.com
首页 > 其他好文 > 详细

SmartImageView

时间:2016-01-21 23:57:11      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

==

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);
        }
    }
}

 

==

SmartImageView

标签:

原文地址:http://www.cnblogs.com/graphics/p/5149791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!