实现方法很简单,根据图片文件的宽度与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的专栏 核心代码
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){ Drawable d = getDrawable();if(d!=null){// ceil not round – avoid thin vertical gaps along the left/right edgesintwidth = MeasureSpec.getSize(widthMeasureSpec);//高度根据使得图片的宽度充满屏幕计算而得intheight = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth()); setMeasuredDimension(width, height); }else{super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
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:
<?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android”> <solid android:color=”@color/white”/> <stroke android:width=”2.0dip” android:color=”#99D9D9D9″ /> <corners android:radius=”2.0dip” /> <padding android:left=”5.0dip” android:top=”5.0dip” android:right=”5.0dip” android:bottom=”5.0dip” /> </shape>
ImageView属性说明:
1、类概述
显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。
2、XML属性
原文地址: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倍