ImageView的ScaleType会忽略padding。

32

我有一个ImageView,它的背景是白色的,并且设置了1dp的内边距,这样就创建了一个类似边框的效果,这正是我想要的结果。

但是,如果我将scaleType设置为centerCrop,它会忽略顶部和底部的填充。
因此,左右两侧仍然有边框,但顶部和底部没有了。

有没有人有什么办法可以阻止这种情况发生? 或者另一种快速创建图像周围边框的方法。我在自定义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" />

android:scaleType="fitxy" 检查 - Khan
2
不,那会拉伸图像,我正在尝试防止这种情况发生。 - TheLD
同时设置android:adjustViewBounds="true"仍然会拉伸图像,因此请改用android:background而不是android:src。 - Khan
4个回答

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" />

你只需要添加 android:cropToPadding

android:cropToPadding="true"

然后imageView将会遵守你选择的padding。


3
欢迎并感谢提供备选答案,Manolo。虽然我无法测试它,但感觉它非常正确。 - Maarten Bodewes
7
请注意,这个属性从API Level 1开始就存在了,但这个方法只在JellyBean版本以后才可用。 - Chris.Jenkins
太棒了,找不到任何解决方案。 - Anton
1
谢谢!你会认为他们会在ImageView.ScaleType的文档中提到这一点... - Reuben Scratton
在Android 4.4.2上,即使使用android:cropToPadding="true"也无法正常工作,如果在设置填充后设置背景资源。因此,您必须先设置背景资源,然后再设置填充。 请参见Matt McMinn的答案:https://dev59.com/IGkw5IYBdhLWcg3wKXX_#15060962 - Harry

2

您可以通过设置 android:background="@drawable/edit_border" 来为其添加边框。

以下是您的 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>

已经得到了边框,但是我的横向ImageView中的肖像图片仍然没有显示顶部和底部的边框。 - TheLD

0

@Manolo Garcia的答案是正确的。 这对我在使用边框、比例类型居中裁剪和填充的动态图像视图方面非常有帮助。

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);

-9

我刚遇到了同样的问题,我的解决方法是添加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"/>

谢谢,它起作用了。我以为在GridView中不能使用额外的LinearLayout,结果我可以。 - TheLD
3
不是所有情况下都可行。Manolo Garcia 给出了正确的答案。 - Makibo
1
Manolo Garcia的答案是正确的,这个右边距没有意义。 - Guilherme Oliveira
1
这不是正确的答案,Manolo Garcia的解决方案是正确的。 - michaelk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接