32

I have a Imageview set with a white background and 1dp padding, this creates a border-like effect, which is the wanted result.

Now if I set the scaleType to centerCrop it ignores the padding on the top and bottom.
So I still have my border on the left and right side, but not on the top and bottom.

Anyone with an idea to stop this from happening? Or another quick way to create a border around images. I use it for my custom gridview

   <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:contentDescription="@string/test"
            android:padding="1dp"
            android:src="@drawable/some_photo"
            android:scaleType="centerCrop" />
Charles
  • 50,943
  • 13
  • 104
  • 142
TheLD
  • 2,211
  • 4
  • 21
  • 26

4 Answers4

176
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:contentDescription="@string/test"
    android:padding="1dp"
    android:src="@drawable/some_photo"
    android:cropToPadding="true"
    android:scaleType="centerCrop" />

You only need to add android:cropToPadding

android:cropToPadding="true"

and then the imageView is going to respect the padding that you choose.

aleb
  • 2,490
  • 1
  • 28
  • 46
Manolo Garcia
  • 3,785
  • 2
  • 19
  • 19
  • 7
    note: that the attribute has been present since api level 1, but the method has only been available since JellyBean. – Chris.Jenkins Feb 21 '13 at 00:34
  • On Android 4.4.2 also with android:cropToPadding="true" it does not work, if you set a background resource after setting the padding. So you have first to set the background resource and then set the padding. See Matt McMinn's answer: http://stackoverflow.com/a/15060962/676945 – Harry Aug 22 '16 at 15:36
2

You can give border by setting android:background="@drawable/edit_border"

And your edit_border:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke
        android:width="2dp"
        android:color="#EBDDE2" />

    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="4dp" />

    <gradient
        android:centerColor="@color/white" 
        android:endColor="@color/white"
        android:startColor="@color/white" />

    <corners android:radius="8dp" />

</shape>
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • Did get the border but the portrait pictures in my landscape Imageviews still don't show the top and bottom border – TheLD Jun 01 '12 at 19:17
0

@Manolo Garcia's Answer is correct. That helped me in dynamic imageview using border, Scale Type Center Crop and padding.

final ImageView imageView = new ImageView(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200,200);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
imageView.setAdjustViewBounds(true);
imageView.setCropToPadding(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setBackgroundResource(R.drawable.img_blue_border);
imageView.setPadding(5, 5, 5, 5);
SeniorJD
  • 6,946
  • 4
  • 36
  • 53
-9

I just encountered the same problem, for my workaround will be adding the android:layout_marginRight="5dip"

<ImageView
    android:layout_width="40dp"
    android:layout_height="30dp"
    android:background="#ffffffff"
    android:src="@drawable/liveview_logo"
    android:layout_marginRight="5dip"/>
Amblll
  • 88
  • 1
  • 11