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" />
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"/>
Answer:
<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.
Answer:
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>
Answer:
@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);
Tags: androidandroid, image, view