代码笔记 | 宽度与屏幕相等,高度根据图片拉伸后做适配

 
public class ScaleScreenImageView extends ImageView {
      
    public ScaleScreenImageView(Context context) {
        super(context);
    }
      
    public ScaleScreenImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
  
    public ScaleScreenImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
      
    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        if (getWidth() > 0) {
            float es = (float) getWidth() / (float)bm.getWidth();
            int height = (int) (bm.getHeight() * es);
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = height;
            setLayoutParams(params);
        }
    }
  
}

你可能感兴趣的:(图片,代码笔记)