2

I am trying to apply image on an image view instance...but it doesnt cover it properly... please advise

here it is my image view code:

    android:id="@+id/imageViewVessel"
    android:layout_width="fill_parent"
    android:scaleType="fitStart"
    android:layout_height="170dip"
    android:src="@drawable/vessel"

EDIT by kcoppock: Adding code from devaditya's comment below

TableRow rowImage = new TableRow(this); 
rowImage.setBackgroundColor(Color.GRAY); 
rowImage.setMinimumHeight(150); 
rowImage.setGravity(Gravity.CENTER); 
rowImage.setMinimumWidth(LayoutParams.FILL_PARENT); 
ImageView imgViewVessel=new ImageView(this); 
imgViewVessel.setImageResource(R.drawable.vessel); 
imgViewVessel.setMinimumHeight(150); 
imgViewVessel.setMinimumWidth(LayoutParams.FILL_PARENT); 
imgViewVessel.setScaleType(ScaleType.FIT_XY); 
rowImage.addView(imgViewVessel);   
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
devaditya
  • 321
  • 9
  • 21

2 Answers2

8

To expand on Gao's answer, you do need to set a scaleType for your ImageView, but it is unlikely that fitXY is the scaleType that you are looking for. You can find the complete list at the above link, but a few of the most common are:

  • centerCrop: This will maintain the aspect ratio of the image, filling the frame entirely, but cropping off either the left and right, or top and bottom of the if the aspect ratio of the frame and source image are different.

  • centerInside: This also maintains the aspect ratio, but the image is scaled to fit entirely within the view, so that the longest edge is the same size as the frame. This can give you a letterbox type of effect if the aspect ratios of the frame and source image are different. fitStart and fitEnd are the same scaling method, but they have different placement of the image (top-left and bottom-right, respectively).

  • fitXY: This one should only be used if disproportionate scaling does not affect the graphic. In the case of bitmap graphics, this is almost always bad to use. This sets the width of the source image to the width of the view, and the height of the source image to the height of the view, without maintaining the aspect ratio of the source image.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • Coppock good suggestions...I tried all of them but I am not able to adjust the width according to the base...have anyone done the similar thing?? – devaditya Dec 21 '10 at 05:04
  • @ohhorob: Really? Good to know, I actually didn't know that. @devaditya: What exactly are you trying to do that NONE of these can accomplish? I honestly don't understand. – Kevin Coppock Dec 21 '10 at 06:51
  • I am trying to show that image in a row of table layout.I am sending my code – devaditya Dec 21 '10 at 09:10
  • TableRow rowImage = new TableRow(this); rowImage.setBackgroundColor(Color.GRAY); rowImage.setMinimumHeight(150); rowImage.setGravity(Gravity.CENTER); rowImage.setMinimumWidth(LayoutParams.FILL_PARENT); ImageView imgViewVessel=new ImageView(this); imgViewVessel.setImageResource(R.drawable.vessel); imgViewVessel.setMinimumHeight(150); imgViewVessel.setMinimumWidth(LayoutParams.FILL_PARENT); imgViewVessel.setScaleType(ScaleType.FIT_XY); rowImage.addView(imgViewVessel); – devaditya Dec 21 '10 at 09:11
  • I'm not familiar with using TableLayouts, but I've edited your code into your initial post, maybe someone else can offer insight. I imagine is something to do with the cell sizing rather than a problem with the ImageView. – Kevin Coppock Dec 21 '10 at 14:14
3

You can set scale type in the layout file : android:scaleType="fitXY" or call setScaleType with fitXY.

Gao Yuesong
  • 144
  • 4