package com.test.android.imageview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class MyImageView extends ImageView {
public MyImageView(Context context) {
this(context, null);
}
public MyImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = MeasureSpec.getSize(widthMeasureSpec);
Drawable d = getDrawable();
Bitmap b = ((BitmapDrawable) d).getBitmap();
float scale = width / (float) b.getWidth();
int height = (int) (scale * b.getHeight());
final int exactWidth = MeasureSpec.makeMeasureSpec(width,
MeasureSpec.EXACTLY);
final int exactHeight = MeasureSpec.makeMeasureSpec(height,
MeasureSpec.EXACTLY);
super.onMeasure(exactWidth, exactHeight);
}
}
调整图片大小适应屏幕的imagview类
最新推荐文章于 2024-10-15 15:10:27 发布
本文介绍了一个自定义的Android ImageView组件——MyImageView,该组件能够自动计算图片的显示尺寸,确保图片在保持原始宽高比例的同时充满整个ImageView。通过重写onMeasure方法并调整图片的测量规格来实现这一功能。
776

被折叠的 条评论
为什么被折叠?



