Android ImageView adjusting parent's height and fitting width

前端 未结 5 518
终归单人心
终归单人心 2020-11-28 02:06

Update : I solved this issue by using the method described in this answer

I\'m a bit stuck with this issue, which I think should be pretty simple.

5条回答
  •  执念已碎
    2020-11-28 02:34

    As I mentioned in a comment, I subclassed the ImageView. I found my code, here you go :

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

提交回复
热议问题