4

I have my imageview declared like this:

<ImageView
        android:id="@+id/category_image_top"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:maxHeight="170dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:background="@drawable/image_placeholder"
        />

And this is how I set my category_image_top in asynctask (on onPostExecute)

imageView.setImageBitmap(image);

When image is set, imageview suddenly gets a margin of 4px. But when I remove android:background="@drawable/image_placeholder" from XML everything is fine?!

BTW: image_placeholder is a 9-patch image, if this makes any difference.

Any ideas why this happens?

UPDATE: I've tried placing background as a solid color and then no margins appear when image is loaded. I've also tried placing another 9-patch image and when I do so margins appear again. So it must be something with background as an image

UPDATE2: Maybe it's an android bug like this guy points out? https://stackoverflow.com/a/8340745/581531

Community
  • 1
  • 1
paxx
  • 1,079
  • 2
  • 12
  • 26
  • try placing a parent linear layout with only a background color on it. Something different to your image and the background of the app eg bright green. If the image has a transparent 4px border as suggested you will only see the green when the setImageBitmap is called (I'd put this in a button call so you can trigger it on demand to find the issue). If a parent of the imageview has a margin you will not see the green. This should help try to identify the problem. Honestly the 4px transparent border is more likely. – Michael Allen Jul 26 '12 at 09:59
  • This is the deal.. If I put a solid color as background to my imageview then everything is OK. I tried what you suggested but my background image has no transparent pixels so my linearlayout background isn't seen "through" imageview – paxx Jul 26 '12 at 10:13
  • It sounds like your 9 Patching is wrong. Is the 9 patching working? – Todd Davies Jul 26 '12 at 10:40
  • I've used Android 9-patch tool (Draw 9-patch) and it looks good in it. Images is displayed properly it just interferes with my src image. Should src image override all background stuff? – paxx Jul 26 '12 at 10:46

3 Answers3

1

I can guess that image_placeholder has 4px transparent margin

logcat
  • 3,435
  • 1
  • 29
  • 44
1

OK. Solution was found, maybe it's not the prettiest one but hey, it work! :)

In my view I've just added an arbitrary View that is a holder for my image. On top of it i have my ImageView so when the image is loaded placeholder (View) get covered. This is my view (or at least part that matters):

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >

        <View 
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:background="@drawable/image_placeholder"
            />

        <ImageView
            android:id="@+id/article_image"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:adjustViewBounds="true" 
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            />
...
paxx
  • 1,079
  • 2
  • 12
  • 26
0

The element that contains the image view may have a margin, have you checked that?

Change android:background="@drawable/image_placeholder" to android:src="@drawable/image_placeholder"

Also add the following: android:scaleType="centerCrop"

If centerCrop doesn't work, try the other ones listed here.

Todd Davies
  • 5,484
  • 8
  • 47
  • 71
  • nope.. no margin.. if I remove background image displays properly – paxx Jul 26 '12 at 10:02
  • if I do this my 9-patch is stretched really weirdly. I have a fixed logo in the middle of the 9-patch, and if I set it to src it gets stretched, but if I set it as background it looks OK :S – paxx Jul 26 '12 at 10:10
  • if I set my 9-patch as a src it gets distorted (blurry and fixed content is scaled). Try all the scale types but non of them fits – paxx Jul 26 '12 at 12:00
  • Are you sure you've done the 9 patching right? Maybe just try it without the 9 patching – Todd Davies Jul 26 '12 at 12:03