Android ImageView调整父级的高度和拟合宽度

Jul*_*ien 62 android resize imageview

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

我对这个问题有点困惑,我认为应该很简单.

所以我的应用程序下载了一个图像,并在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>
Run Code Online (Sandbox Code Playgroud)

和代码:

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);

    }
Run Code Online (Sandbox Code Playgroud)

期望:

期望的结果

结果:

目前的结果

Ser*_*nky 147

感谢@Julien和@js.这是ImageView的完整解决方案,即使位图小于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);
         }
    }

}
Run Code Online (Sandbox Code Playgroud)

您可以在xml布局中使用此类而不是ImageView.

<com.example.ResizableImageView
    android:id="@+id/banner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/banner" />
Run Code Online (Sandbox Code Playgroud)


Mat*_*lis 51

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


Jul*_*ien 8

正如我在评论中提到的,我将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);
            }

    }
Run Code Online (Sandbox Code Playgroud)