【问题标题】:How do you fill imageview to screen in android你如何在android中将imageview填充到屏幕上
【发布时间】:2018-08-09 12:18:22
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="galleryimages.galleryimages.Main2Activity"
    android:background="#000000"
    android:id="@+id/linear1">


    <ImageView
        android:id="@+id/iv_image2"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_above="@+id/button1"
        android:layout_weight="0"
        android:layout_gravity="center"
        tools:ignore="InvalidId" />


    <ImageButton
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:adjustViewBounds="false"
        android:background="#ffffff"
        android:gravity="bottom"
        android:src="@drawable/ic_delete_black_24dp" />


</RelativeLayout>

我希望图像填满整个屏幕。我应该为 18:9 比例的手机编写单独的代码吗?另外,如果我删除一张图片,它不会与谷歌照片同步。

【问题讨论】:

  • 鉴于您不想拥有黑色,我建议添加android:scaleType="centerCrop" 。这将适合图像,因此它会填满屏幕。使用反映手机纵横比的图像将减少裁剪。使用 fitXY 会导致图像从其原始纵横比变形。

标签: android android-layout android-studio


【解决方案1】:

将此属性添加到 ImageView:

android:scaleType="centerCrop"  


android:scaleType="fitXY"

【讨论】:

  • 我试过了。它适用于某些图像。如果图像是水平图像,则图片被缩放
  • 我希望图像填满整个屏幕这就是你想要的
  • 然后发布另一个问题并解释您想要什么,提供所有可能有用的代码或图像。
【解决方案2】:

您可以从此代码中使用:

 <ImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="center"
                android:layout_marginBottom="64dp"
                android:adjustViewBounds="true" />

其他方式:

DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int height = displayMetrics.heightPixels;
            int width = displayMetrics.widthPixels;
            int ratio = imgTop.getDrawable().getIntrinsicWidth() / imgTop.getDrawable().getIntrinsicHeight();
            int newheght = width / ratio;
            imgTop.getLayoutParams().height = newheght;

【讨论】:

    【解决方案3】:

    这总是对我有用。在 ImageView 属性中添加:

       android:scaleType="fitCenter"
    

    【讨论】:

      【解决方案4】:

      我认为您必须添加以下代码:

       android:adjustViewBounds="true"
      

      在xml文件中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-03
        • 2017-09-13
        • 2015-07-02
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 2015-09-20
        • 2018-06-08
        相关资源
        最近更新 更多