Reputation: 3958
The idea is to either display a photo or - if none is defined - a small icon symbolizing it.
I have defined the following ImageView in the layout:
<ImageView
android:id="@+id/imgPic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@+string/strImgPic"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:saveEnabled="false"
android:src="@drawable/pic_icon" />
So the ImageView is pre-initialized with the 'icon' PNG.
The code to load it:
try {
imgURL = vinfo.getString("mainpic_url");
if (imgURL.length() > 1) {
imgURL = getString(R.string.urlPicsFull) + imgURL;
ImageView imgPic = (ImageView) findViewById(R.id.imgPic);
ImageDownloader img = new ImageDownloader();
img.download(imgURL, imgPic);
}
} catch (..) {
(..)
ImageDownloader() basically does nothing else than retrieving the image asynchronously. The problem however is that if there is NO image ( imgURL.length()==0 ) , the icon is displayed with a very large 'border' in the middle of an empty square in the layout.
I would like the image to be scaled down (or up) to the width of the parent view, but the icon to display as it is (about 90 dip wide and high).
Any suggestion on how to realize that?
Upvotes: 0
Views: 452
Reputation: 4351
Check out the android:scaleType tag of the ImageView
. For your case if you add
android:scaleType="fitXY"
in your ImageView
definition in the layout, and the image will be adjusted to your ImageView
size. Something like this:
<ImageView
android:id="@+id/imgPic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@+string/strImgPic"
android:paddingRight="10dip"
android:scaleType="fitXY"
android:paddingTop="10dip"
android:saveEnabled="false"
android:src="@drawable/pic_icon" />
However this will not maintain the aspect ratio so the image may be distorted. If you do not want that, check the other values that the android:scaleType
tag can take in the link provided.
Upvotes: 1