java Android ImageView 比例尺类型 centerCrop

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17808527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 15:04:14  来源:igfitidea点击:

Android ImageView scale type centerCrop

javaandroidandroid-layout

提问by Erdin? ?zdemir

I'm creating an activity on Android. I want to show a background image in it. I put a ImageView into my main RelativeLayout.

我正在 Android 上创建一个活动。我想在其中显示背景图像。我将一个 ImageView 放入我的主要相对布局中。

<RelativeLayout
    android:id="@+id/relativeLayoutMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/bgImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:src="@drawable/bg4"
        android:scaleType="centerCrop" />
</RelativeLayout>

It makes view like this.

它使这样的观点。

enter image description here

在此处输入图片说明

I need to make view like this enter image description here

我需要这样看 在此处输入图片说明

How can I do it?

我该怎么做?

回答by Fedir Tsapana

Try set ImageView attribute ScaleType=fitEnd?

尝试设置 ImageView 属性ScaleType=fitEnd

回答by Zala Janaksinh

use this code.it's give the out-put as you want.
Code

使用此代码。它可以根据需要提供输出。
代码

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

    <RelativeLayout
        android:id="@+id/relativeLayoutMain"
        android:layout_width="150dp"
        android:background="@android:color/background_dark"
        android:layout_height="150dp" >

        <ImageView
            android:id="@+id/bgImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>

</RelativeLayout>

one advice if u give the imageview width fill parent then it's contain whole space according to main container. Best Luck

一个建议,如果你给 imageview 宽度填充父项,那么它根据主容器包含整个空间。好运

回答by g00dy

Set the android:background="@drawable/bg4"in the RelativeLayoutlike that:

设置android:background="@drawable/bg4"RelativeLayout这样的:

The whole XML should look like that:

整个 XML 应如下所示:

<RelativeLayout
    android:id="@+id/relativeLayoutMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:background="@drawable/bg4
    android:orientation="vertical" >
</RelativeLayout>

回答by Ivo Beckers

First, I believe there are a couple of things wrong in the xml:

首先,我认为 xml 中有一些错误:

RelativeLayout does not make use of an orientation, you should remove that

RelativeLayout 不使用方向,您应该删除它

Is this your full xml file? If so then the alignParentLeft in the RelativeLayout doesn't do anything because that is an attribute you use if it's parent is a RelativeLayout.

这是您的完整 xml 文件吗?如果是这样,那么RelativeLayout 中的alignParentLeft 不会做任何事情,因为如果它的父项是RelativeLayout,那么这是您使用的属性。

fill_parent is the deprecated name of match_parent, you should use that.

fill_parent 是 match_parent 的弃用名称,您应该使用它。

I also believe that code you have would center the image on the screen putting anything that is to big evenly on both sides and not at all what you said it does.

我也相信你拥有的代码会将图像放在屏幕上的中心,在两边均匀地放置任何大的东西,而不是你所说的。

If you just want a background you can just use android:background="@drawable/bg4"in the RelativeLayout

如果你只想要一个背景,你可以android:background="@drawable/bg4"在 RelativeLayout 中使用

EDIT: Changing the layoutparams of the ImageView to wrap_content both height and width and removing the scaleType alltogether might make it like you wanted it to be.

编辑:将 ImageView 的 layoutparams 更改为 wrap_content 的高度和宽度并一起删除 scaleType 可能会使其像您想要的那样。