将以下类添加到您的项目中并像这样更改您的布局
看法
<my.package.name.AspectRatioImageView
android:layout_centerHorizontal="true"
android:src="@drawable/my_image"
android:id="@+id/my_image"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:adjustViewBounds="true" />
班级
package my.package.name;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* ImageView which scales an image while maintaining
* the original image aspect ratio
*
*/
public class AspectRatioImageView extends ImageView {
/**
* Constructor
*
* @param Context context
*/
public AspectRatioImageView(Context context) {
super(context);
}
/**
* Constructor
*
* @param Context context
* @param AttributeSet attrs
*/
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Constructor
*
* @param Context context
* @param AttributeSet attrs
* @param int defStyle
*/
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Called from the view renderer.
* Scales the image according to its aspect ratio.
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}