3 回答
TA贡献1856条经验 获得超5个赞
不使用任何自定义类或库:
<ImageView
android:id="@id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
scaleType="fitCenter" (省略时为默认)
将使其宽度达到父级允许的范围,并根据需要向上/向下缩放以保持宽高比。
scaleType="centerInside"
如果的固有宽度src小于父级宽度,
则会使图像水平居中
如果的固有宽度src大于父级宽度,
则会使其达到父级允许的宽度,并缩小比例并保持宽高比。
不管您使用android:src还是ImageView.setImage*方法,密钥都可能是adjustViewBounds。
TA贡献1155条经验 获得超0个赞
我曾经有过类似的问题。我通过制作自定义ImageView解决了它。
public class CustomImageView extends ImageView
然后覆盖imageview的onMeasure方法。我相信我做了这样的事情:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
Drawable drawable = getDrawable();
if (drawable == null) {
setMeasuredDimension(0, 0);
} else {
float imageSideRatio = (float)drawable.getIntrinsicWidth() / (float)drawable.getIntrinsicHeight();
float viewSideRatio = (float)MeasureSpec.getSize(widthMeasureSpec) / (float)MeasureSpec.getSize(heightMeasureSpec);
if (imageSideRatio >= viewSideRatio) {
// Image is wider than the display (ratio)
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int)(width / imageSideRatio);
setMeasuredDimension(width, height);
} else {
// Image is taller than the display (ratio)
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = (int)(height * imageSideRatio);
setMeasuredDimension(width, height);
}
}
} catch (Exception e) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
这将拉伸图像以适合屏幕,同时保持宽高比。
- 3 回答
- 0 关注
- 831 浏览
添加回答
举报