相当于ImageView scaleType'centerCrop'到位图可绘制引力

时间:2013-03-13 09:38:31

标签: android android-layout drawable

我创建了一个带有标题图片的活动。此标头图像最初使用ImageView在Activity的布局xml中创建,其中scaleType设置为centerCrop。这就是我想要的,它使图像居中,在纵向模式下左右剪切,以横向模式显示所有图像。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="30dp"
    android:paddingBottom="6dp"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:background="#919EAC"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/header_image"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:contentDescription="@string/header_description"
    android:scaleType="centerCrop"
    android:src="@drawable/header" />

    .... <other views> ...

我想用背景drawable替换它,以便我可以使用标题图像空间来显示数据,这样可以节省我在不同活动中重复相同的布局。

因此我创建了一个drawable,我可以在android:background属性中引用:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#919EAC" />
            </shape>
        </item>
        <item>
            <bitmap 
                android:antialias="true"
                android:gravity="top|center_horizontal"
                android:src="@drawable/header" />
        </item>
    </layer-list>

此drawable定义了布局定义的彩色背景,它定义了一个标题图像,否则将作为ImageView包含在布局中。

然而现在我的图像被缩放,而我希望它被裁剪。

检查Android文档我尝试了其他重力模式,例如

    android:gravity="top|clip_horizontal"

但它似乎仍然以不同于图像视图的方式显示/缩放。

背景drawable的正确定义是什么?

4 个答案:

答案 0 :(得分:3)

我用装饰器解决了这个问题,装饰器保持了底层drawable的宽高比:https://gist.github.com/rudchenkos/e33dc0d6669a61dde9d6548f6c3e0e7e

不幸的是,没有办法从XML应用它,所以我在我的启动活动的最开始时这样做:

public class SplashActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Drawable bg = getResources().getDrawable(R.drawable.screen);
        getWindow().setBackgroundDrawable(new CenterCropDrawable(bg));

        ...
    }
}

这大致相当于在活动主题中将drawable指定为android:windowBackground

答案 1 :(得分:2)

要在代码中居中裁剪位图,请执行以下操作:

public static Bitmap cropCenter(Bitmap bmp) {
    int dimension = Math.min(bmp.getWidth(), bmp.getHeight());
    return ThumbnailUtils.extractThumbnail(bmp, dimension, dimension);
}

我不知道如何以任何其他方式将centermap映射到xml。

希望它可以帮助任何人。

我从这篇文章中找到了这个解决方案: Android Crop Center of Bitmap

答案 2 :(得分:0)

我不知道如何使用后台进行操作。您可以尝试其他解决方案:

1)在图像上显示某些内容的最简单方法是将它们放在一些布局中以便它们重叠。例如,您可以使用具有ImageView尺寸的RelativeLayout,ImageView在两个维度中都是match_parent,在中心是TextView。

2)您可以将Imageview保存在不同的布局中,例如header.xml,并在需要的地方使用 include header.xml

3)如果要在ImageView顶部显示的布局在任何地方都相同,那么您可以使用ImageView创建一个自定义布局,并在中心创建一个TextView,并在任何地方使用该布局。您可以在此自定义布局上设置TextView字符串。您可以阅读更多自定义布局,它们很简单。

答案 3 :(得分:-6)

使用scaleType = "centerCrop"您获得了所需的结果,裁剪图像的边缘并显示适合该尺寸的中心部分

对于ImageView:

ImageView具有scaleType属性,仅适用于要设置为显示的源的图像。即好像它是TextView,然后Text是它的来源。

即使您可能已经注意到 scaleType不适用于设置为背景的图像,该图像仅适用于在运行时以XML格式提供的图像。

布局

Layout可以显示背景图片,但是没有为布局提供任何属性,可以处理背景图片并根据属性的值进行缩放。

所以你应该停止从ImageView询问Layout的设施,因为它没有它。

您问题的两种不同解决方案

1)在不同的可绘制文件夹中保留适当的背景标题图像并使用它,这样就不需要缩放它的运行时间,因为你已经为不同的尺寸开发了图像。

2)您需要重新设计XML,如下面的示例

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="@drawable/subway"
        android:scaleType="centerCrop" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
         />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>