代码之家  ›  专栏  ›  技术社区  ›  huong

(Android-xml)Gridview和ImageView缩放类型中心裁剪

  •  1
  • huong  · 技术社区  · 12 年前

    我正在尝试在 GridView ,并且我只想裁剪此列表中图像的中心。下面是每个图像项目的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/place_image"
            android:src="@drawable/ic_res"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="centerCrop"/>
    

    这是包含网格视图的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <LinearLayout android:id="@+id/upload_view"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical"
                      android:background="#f6f6f6"
                      android:padding="10dp"
                      android:visibility="gone">
            <LinearLayout android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:orientation="horizontal">
                <ImageButton android:id="@+id/photo_select"
                        android:layout_width="50dp"
                        android:layout_height="40dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/ic_photo"/>
                <EditText android:id="@+id/photo_caption"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:hint="@string/caption"
                          android:inputType="textMultiLine"/>
            </LinearLayout>
            <TextView  android:id="@+id/photo_path"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="" />
    
            <Button android:id="@+id/photo_upload"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/upload_photo"
                    android:textStyle="bold"
                    android:textColor="#ffffff"
                    android:layout_gravity="right"/>
        </LinearLayout>
        <TextView android:id="@+id/no_photos"
                  android:layout_height="wrap_content"
                  android:layout_width="match_parent"
                  android:gravity="center"
                  android:textStyle="bold"
                  android:textSize="20dp"
                  android:text="@string/no_photos"
                  android:visibility="gone" />
        <GridView android:id="@+id/grid_photos"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="20dp"
                android:columnWidth="120dp"
                android:numColumns="auto_fit"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:stretchMode="columnWidth"
                android:gravity="center" />
    </LinearLayout>
    

    这是我得到的结果:

    enter image description here

    这很奇怪,因为图像都是以一种奇怪的方式裁剪的。以下是我的一张原始图片:

    enter image description here

    我从来没有遇到过这个问题,所以如果你们中有人熟悉这个问题,请帮我找出原因。非常感谢。

    附言:我想这可能只是xml布局的问题,所以我认为没有必要在这里包含我的android源代码。但如果真的是这样,请让我知道问题可能来自哪里,我会用我的代码编辑这个问题。

    1 回复  |  直到 12 年前
        1
  •  3
  •   Triode    12 年前

    原因是 GridView 清除为平铺设置的所有布局参数,并设置自己的布局参数,以便更好地添加 ImageView 内部 RelativeLayout 。这与所有 AdapterView 与ListView一样, Gallery