书法字典:https://www.shufadict.com

SmartImageView

==

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

 

==

作者:zdd
出处:http://www.cnblogs.com/graphics/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

posted on 2016-01-21 22:30  翰墨小生  阅读(914)  评论(0编辑  收藏  举报

导航

书法字典:https://www.shufadict.com