实现方法很简单,根据图片文件的宽度与ImageView的宽度比例关系算出ImageView的高度。

package com.etongwl.commonlibs.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;

/*
 * @Override
    public void setImageDrawable(final Drawable drawable) {
        super.setImageDrawable(drawable);
        if (drawable != null) {
            post(new Runnable() {
                @Override public void run() {
                    float mWidth = getWidth();
                    float pWidth = drawable.getBounds().width();
                    float pHeight = drawable.getBounds().height();
                    int mHeight = (int) ((mWidth / pWidth) * pHeight);

                    LayoutParams params = getLayoutParams();
                    params.height = mHeight;
                    params.width = (int) mWidth;
                    setLayoutParams(params);
                }
            });
        }
    }
    首先, 如果drawable不为空, 则通过drawable.getBounds()去获取drawable边界的宽和高. 在根据ImageView的宽与drawable的宽的比例去计算高度, 最后通过LayoutParams设置ImageView的高度
 *
 */

/*
<com.etongwl.commonlibs.view.AdaptiveImageView
          android:id="@+id/detail_image"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginTop="15dp"
          android:scaleType="centerCrop"
          android:background="@drawable/diandianxinwen"
          android:visibility="visible" />
 */
/**
 * 按比例缩放
 */

@SuppressLint("AppCompatCustomView")
public class AdaptiveImageView extends ImageView {
    // 控件默认长、宽
    private int defaultWidth = 0;
    private int defaultHeight = 0;
    // 比例
    private float scale = 0;

    public AdaptiveImageView(Context context) {
        super(context);
    }

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

    public AdaptiveImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override

    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }

        this.measure(0, 0);
        if (drawable.getClass() == NinePatchDrawable.class)
            return;

        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        if (bitmap.getWidth() == 0 || bitmap.getHeight() == 0) {
            return;
        }
        if (defaultWidth == 0) {
            defaultWidth = getWidth();
        }
        if (defaultHeight == 0) {
            defaultHeight = getHeight();
        }
        scale = (float) defaultWidth / (float) bitmap.getWidth();
        defaultHeight = Math.round(bitmap.getHeight() * scale);
        ViewGroup.LayoutParams params = this.getLayoutParams();
        params.width = defaultWidth;
        params.height = defaultHeight;
        this.setLayoutParams(params);
        super.onDraw(canvas);
    }
}

简单实现ImageView宽度填满屏幕,高度自适应的两种方式

两种方式 1.重写View的onMeasure方法参考这里easion_zms的专栏 核心代码

2.设置ImageView的属性: //宽度填满屏幕 android:layout_width=”match_parent” android:scaleType=”fitXY” android:layout_height=”wrap_content” //保持比例,一定要设置 android:adjustViewBounds=”true”

 

Android ImageView图片自适应

<ImageView

   android:id=”@+id/dynamic_item_image”

   android:layout_width=”wrap_content”

   android:layout_height=”wrap_content”

   android:layout_gravity=”top”

   android:layout_marginTop=”5dip”

   android:adjustViewBounds=”true”

   android:background=”@drawable/imageview_background” />
另外,android:background=”@drawable/imageview_background”是给图片加了一个边框,其中

imageview_background.xml:
<shape  xmlns:android=”http://schemas.android.com/apk/res/android”>

ImageView属性说明:

1、类概述

                显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

2、XML属性

    属性名称
  

</td>

<td>
  

    描述
  

</td>
    android:adjustViewBounds
  

</td>

<td>
  

    是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。
  

</td>
    android:cropToPadding
  

</td>

<td>
  

    是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分:
  

  
  

    [<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225808_136.jpg" alt="Android ImageView图片自适应" width="153" height="80" />](http://static.oschina.net/uploads/img/201405/06143901_RjRg.jpg) 
  

  
  

    &nbsp;
  

</td>
    android:maxHeight
  

</td>

<td>
  

    设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:
  

  
  

    1) 设置setAdjustViewBounds为true;
  

  
  

    2) 设置maxWidth、MaxHeight;
  

  
  

    3) 设置设置layout_width和layout_height为wrap_content。
  

</td>
    android:maxWidth
  

</td>

<td>
  

    设置View的最大宽度。同上。
  

</td>
    android:scaleType
  

</td>

<td>
  

    设置图片的填充方式。
  

  
  <table class="ke-zeroborder " cellspacing="0" cellpadding="0">
    <tr>
      <td>
        

          matrix
        

      </td>
      
      <td>
        

        

      </td>
      
      <td>
        

          用矩阵来绘图
        

      </td>
      
      <td>
         
      </td>
    </tr>
    
    <tr>
      <td>
        

          fitXY
        

      </td>
      
      <td>
        

          1
        

      </td>
      
      <td>
        

          拉伸图片(不按比例)以填充View的宽高
        

        
        

          [<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225808_94.png" alt="Android ImageView图片自适应" width="120" height="30" />](http://static.oschina.net/uploads/img/201405/06143901_f9F7.png)
        

        
        

          &nbsp;
        

      </td>
      
      <td>
        

          layout_
        

        
        

          height
        

        
        

          :30px
        

        
        

          layout_
        

        
        

          width
        

        
        

          :120px
        

      </td>
    </tr>
    
    <tr>
      <td>
        

          fitStart
        

      </td>
      
      <td>
        

          2
        

      </td>
      
      <td>
        

          按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边
        

        
        

          [<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225808_131.png" alt="Android ImageView图片自适应" width="120" height="30" />](http://static.oschina.net/uploads/img/201405/06143901_FjUd.png)
        

        
        

          &nbsp;
        

      </td>
    </tr>
    
    <tr>
      <td>
        

          fitCenter
        

      </td>
      
      <td>
        

          3
        

      </td>
      
      <td>
        

          按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间
        

        
        

          [<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225808_201.png" alt="Android ImageView图片自适应" width="120" height="30" />](http://static.oschina.net/uploads/img/201405/06143901_c4jh.png)
        

        
        

          &nbsp;
        

      </td>
    </tr>
    
    <tr>
      <td>
        

          fitEnd
        

      </td>
      
      <td>
        

          4
        

      </td>
      
      <td>
        

          按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边
        

        
        

          [<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225808_509.png" alt="Android ImageView图片自适应" width="120" height="30" />](http://static.oschina.net/uploads/img/201405/06143901_aCWp.png)
        

        
        

          &nbsp;
        

      </td>
    </tr>
    
    <tr>
      <td>
        

          center
        

      </td>
      
      <td>
        

          5
        

      </td>
      
      <td>
        

          按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示[<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225808_219.png" alt="Android ImageView图片自适应" width="80" height="60" />](http://static.oschina.net/uploads/img/201405/06143901_Rzoa.png)
        

        
        

          &nbsp;
        

      </td>
      
      <td>
        

          &nbsp;
        

        
        

          layout_
        

        
        

          height
        

        
        

          :60px
        

        
        

          layout_
        

        
        

          width
        

        
        

          :80px
        

        
        

          padding
        

        
        

          :10px
        

        
        

          &nbsp;
        

      </td>
    </tr>
    
    <tr>
      <td>
        

          centerCrop
        

      </td>
      
      <td>
        

          6
        

      </td>
      
      <td>
        

          按比例放大原图直至等于某边View的宽高显示。[<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225808_405.png" alt="Android ImageView图片自适应" width="80" height="60" />](http://static.oschina.net/uploads/img/201405/06143901_JPev.png)
        

        
        

          &nbsp;
        

      </td>
    </tr>
    
    <tr>
      <td>
        

          centerInside
        

      </td>
      
      <td>
        

          7
        

      </td>
      
      <td>
        

          当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。[<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225809_159.png" alt="Android ImageView图片自适应" width="80" height="60" />](http://static.oschina.net/uploads/img/201405/06143902_UCBx.png)
        

        
        

          &nbsp;
        

      </td>
    </tr>
  </table>
</td>
    android:src
  

</td>

<td>
  

    设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小)
  

</td>
    android:tint
  

</td>

<td>
  

    将图片渲染成指定的颜色。见下图:
  

  
  

    [<img loading="lazy" decoding="async" style="border: 0px; max-width: 100%; vertical-align: middle;" title="Android <wbr>ImageView图片自适应" src="http://static.open-open.com/lib/uploadImg/20140506/20140506225809_434.jpg" alt="Android ImageView图片自适应" width="81" height="69" />](http://static.oschina.net/uploads/img/201405/06143902_dMaT.jpg) 左边为原图,右边为设置后的效果,见后面代码。
  

</td>

原文地址:http://blog.sina.com.cn/s/blog_618199e60100y537.html

 

android ImageView 宽度设定,高度自适应

首先,需要给你的ImageView布局加上Android:adjustViewBounds=”true”

<ImageView android:id=”@+id/test_image”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:scaleType=”fitXY”
android:adjustViewBounds=”true”
android:layout_gravity=”center”
android:contentDescription=”@string/app_name”
android:src=”@drawable/ic_launcher” />

 

然后,在代码里设置ImageView.最大宽度和最大高度,因为adjustViewBounds属性只有在设置了最大高度和最大宽度后才会起作用

int screenWidth = getScreenWidth(this);
ViewGroup.LayoutParams lp = testImage.getLayoutParams();
lp.width = screenWidth;
lp.height = LayoutParams.WRAP_CONTENT;
testImage.setLayoutParams(lp);

testImage.setMaxWidth(screenWidth);
testImage.setMaxHeight(screenWidth * 5); 这里其实可以根据需求而定,我这里测试为最大宽度的5倍