Android ImageView调整父母的身高和适应宽度

更新 :我通过使用在这个答案中描述的方法解决了这个问题

我有点卡住这个问题,我觉得应该是非常简单的。

所以我的应用程序下载一个图像,并呈现在一个ImageView,一个RelativeLayout的子元素的位图。 我希望ImageView适合父宽度,并调整它的大小来保持宽高比。

这是我的XML:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/banner" android:layout_width="fill_parent" android:layout_height="wrap_content"></RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 

和代码:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RelativeLayout banner = (RelativeLayout) findViewById(R.id.banner); ImageView imgV = new ImageView(this); imgV.setScaleType(ImageView.ScaleType.CENTER_CROP); // I tried all the scale types : CENTER_INSIDE : same effect, FIT_CENTER : same effect... imgV.setBackgroundColor(0x00FFFF00); imgV.setAdjustViewBounds(Color.BLUE); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); banner.addView(imgV,params); // Some code downloading the image stream bitmap = BitmapFactory.decodeStream(stream); imgV.setImageBitmap(bitmap); } 

期望的:

期望的结果

结果:

当前结果

感谢@Julien和@js。 这是ImageView的完整解决scheme,即使位图比ImageView小,也可以扩展位图高度,保持高宽比。

 public class ResizableImageView extends ImageView { public ResizableImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ Drawable d = getDrawable(); if(d!=null){ // ceil not round - avoid thin vertical gaps along the left/right edges int width = MeasureSpec.getSize(widthMeasureSpec); int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth()); setMeasuredDimension(width, height); }else{ super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } } 

你可以在你的xml布局中使用这个类来代替ImageView。

 <com.example.ResizableImageView android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/banner" /> 

您可能正在寻找xml中的android:adjustViewBounds="true"或Java中的android:adjustViewBounds="true" imageView.setAdjustViewBounds(true)

正如我在评论中提到的,我将这个ImageView分类。 我发现我的代码,在这里你去:

  protected class ResizableImageView extends ImageView { private Bitmap mBitmap; // Constructor public ResizableImageView(Context context) { super(context); } // Overriden methods @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if(mBitmap != null) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = width * mBitmap.getHeight() / mBitmap.getWidth(); setMeasuredDimension(width, height); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } @Override public void setImageBitmap(Bitmap bitmap) { mBitmap = bitmap; super.setImageBitmap(bitmap); } } 

我认为你应该改变你的imgV宽度为“match_parent”

您应该将比例types更改为imgV.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

我对@Seraphim S的解决scheme进行了一些细微的改动,以解决图像可能很宽,视图边界也很宽的情况(例如,将设备旋转到横向模式)。

 public class ResizableImageView extends ImageView { public ResizableImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable d = getDrawable(); if (d != null) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); if (width >= height) { width = (int) Math.ceil((float) height * (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight()); } else { height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth()); } setMeasuredDimension(width, height); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } }