zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }

    ==

  • 相关阅读:
    Django(进阶篇)之model
    RabbitMQ、Memcache、Redis(队列、缓存)
    AJAX总结
    数据库 MySql(二)
    Python操作mysql之SQLAchemy(ORM框架)
    Python操作Mysql
    Tornado框架
    Ubuntu 出现未定义的 curl_init 错误
    ubuntu下apache配置https且强制http转向为https 腾讯云
    Git fetch
  • 原文地址:https://www.cnblogs.com/graphics/p/5149791.html
Copyright © 2011-2022 走看看