3 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
不使用任何自定義類或庫:
<ImageView
android:id="@id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
scaleType="fitCenter" (省略時(shí)為默認(rèn))
將使其寬度達(dá)到父級(jí)允許的范圍,并根據(jù)需要向上/向下縮放以保持寬高比。
scaleType="centerInside"
如果的固有寬度src小于父級(jí)寬度,
則會(huì)使圖像水平居中
如果的固有寬度src大于父級(jí)寬度,
則會(huì)使其達(dá)到父級(jí)允許的寬度,并縮小比例并保持寬高比。
不管您使用android:src還是ImageView.setImage*方法,密鑰都可能是adjustViewBounds。

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
我曾經(jīng)有過類似的問題。我通過制作自定義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);
}
這將拉伸圖像以適合屏幕,同時(shí)保持寬高比。
- 3 回答
- 0 關(guān)注
- 865 瀏覽
添加回答
舉報(bào)