代码之家  ›  专栏  ›  技术社区  ›  Francisco Romero

如何使用XML在Android上放大图像?

  •  1
  • Francisco Romero  · 技术社区  · 11 年前

    我有一张照片 ImageView 但我不知道怎么把它做大 使用XML 。我的图像如下:

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/img"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:src="@drawable/image"/>
    

    我试过换衣服 android:layout_width 和 android:layout_height 属性,但如果我用数字(例如, 200dp width 和 100dp height ) 图片框 它在屏幕的左边,而不是中间。

    我想将图像居中,使其比原始图像大一点。我找不到任何东西可以让它在屏幕中央显示的同时变得更大。

    有可能吗?我该怎么做?

    提前感谢!

    4 回复  |  直到 11 年前
        1
  •  2
  •   IntelliJ Amiya    11 年前

    使用 scaletype 并添加 android:adjustviewbounds="true" 在图像视图部分。

        2
  •  1
  •   Andrew Brooke    11 年前

    保持所需大小作为 ImageView 并更改 gravity 其母公司 center

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/img"
            android:layout_marginTop="40dp"
            android:src="@drawable/image"/>
    
    </LinearLayout>
    

    编辑:

    如果这是你想要的布局

    enter image description here

    那么你只需要添加

    android:layout_gravity="center_horizontal"
    

    到您的 图片框 ,并指定 width 和 height 在里面 dp

        3
  •  1
  •   George Rappel AdminOfThings    6 年前

    有3个属性可以添加到图像视图中:

    android:scaleType=""
    android:scaleX=""
    android:scaleY=""
    

    ScaleType 可以看到类型 here 。基本上,您需要使用 CENTER_CROP .

    scaleX 和 scaleY 使用float作为参数,定义两个轴的比例。参见参考文献 here .

        4
  •  0
  •   Francisco Romero    11 年前

    以上两种代码都有效( Andrew Brooke 和 IntelliJ Amiya ,谢谢!)但现在我找到了一个避免这两种方法的“窍门”。它是为了创建 ImageView 使用 android:minWidth 和 android:minHeight 所以决赛 图片框 将类似于:

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/img"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:minWidth="100dp"
        android:minHeight="70dp"
        android:src="@drawable/image"/>
    

    有了它,你可以将你的 图片框 您想要的尺寸。