package com.hisign.wencai.widget;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* 开发者:hongzhen
* 创建时间: 2019/8/6 11:49
* 类名:ImageViewScale.java
* 描述:根据父控件保持比例缩放的图片展示控件
*/
@SuppressLint("AppCompatCustomView")
public class ImageViewScale extends ImageView {
public ImageViewScale(Context context) {
super(context);
}
public ImageViewScale(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ImageViewScale(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = getDrawable();
if (drawable != null) {
//根据宽度来计算比例,保持比例
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) drawable.getIntrinsicHeight() / (float) drawable.getIntrinsicWidth());
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
在xml文件的ImageView的属性中添加如下代码,正常情况下可以实现图片的自适应,但是也有个例情况。如果出现无法自适应缩放的情况,使用上面的自定义的ImageView控件,重写了onMeasure方法,就可以实现自适应缩放了。
Android:scaleType="fitXY" :填充宽度match_parent
android:adjustViewBounds="true" :高度保持比例