【问题标题】:Equivalent of ImageView scaleType 'centerCrop' to bitmap drawable gravityImageView scaleType 'centerCrop' 等价于位图可绘制重力
【发布时间】:2013-03-01 04:00:20
【问题描述】:

我创建了一个带有标题图像的活动。此标题图像最初是使用 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> ...

我想将其替换为背景可绘制对象,以便我可以使用标题图像空间来显示数据,并且它可以让我在不同的活动中重复相同的布局。

出于这个原因,我创建了一个可在 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>

此可绘制对象定义了布局将定义的彩色背景,并定义了一个标题图像,否则该图像将作为 ImageView 包含在布局中。

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

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

    android:gravity="top|clip_horizontal"

但它的显示/缩放似乎仍然与图像视图不同。

背景可绘制对象的正确定义是什么?

【问题讨论】:

  • 您找到解决问题的方法了吗?我也在为同样的事情苦苦挣扎。
  • “fill_vertical|clip_horizo​​ntal”怎么样
  • @TreyCai 关闭,但不一样...

标签: android android-layout drawable


【解决方案1】:

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

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

我不知道有任何其他方式可以将位图 xml 居中裁剪。

希望对大家有所帮助。

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

【讨论】:

    【解决方案2】:

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

    不幸的是,没有办法从 XML 应用它,所以我在启动 Activity 的一开始就这样做了:

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

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

    【讨论】:

    • 不幸的是,这非常大致相当,因为它同时出现在活动的正常布局中,因此它不能用作初始替换.
    【解决方案3】:

    我不知道如何处理背景。不过,您可以尝试其他解决方案:

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

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

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

    【讨论】:

      【解决方案4】:

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

      对于 ImageView:

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

      甚至您可能已经注意到 scaleType 不适用于设置为背景的图像,它仅适用于在运行时以 XML 或 XML 形式提供的源图像。

      用于布局

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

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

      2 种不同的解决方案

      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> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-07
        • 1970-01-01
        • 2018-08-25
        • 2023-03-15
        • 1970-01-01
        • 2018-08-13
        • 1970-01-01
        • 2019-07-12
        相关资源
        最近更新 更多