代码之家  ›  专栏  ›  技术社区  ›  Mariano L

Imageview未缩放图片

  •  0
  • Mariano L  · 技术社区  · 8 年前

    我有下面的imageview,其图像小于包含此imageview的RelativeLayout。我想缩放图像以适应imageView的宽度,但我无法做到这一点。例如I 与此相关 350dp宽度 :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_button_grey"
        android:clickable="true"
        android:padding="5dp">
    
        <ImageView
            android:id="@+id/image_selector"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:background="@color/black"
            android:src="@drawable/Nachos"
             />
    
        <TextView
            android:id="@+id/image_selector_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/image_selector"
            android:gravity="center_horizontal|center_vertical"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
    
            android:textSize="18sp" />
    
    
    </RelativeLayout>
    

    正如你在图片中看到的那样,图像在中间。fitXY的问题是宽度可以工作,但不会改变高度。我想要像这样的东西, fitX公司 . 我也试过了 中心内部

    enter image description here

    6 回复  |  直到 8 年前
        1
  •  2
  •   Dambakk    8 年前

    我也会使用 centerCrop enter image description here

    如果这对你来说无关紧要,请考虑查看Android中的图像定制: https://developer.android.com/guide/topics/graphics/2d-graphics.html

        2
  •  1
  •   Manthan Patel    8 年前

    如下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="350dp"
        android:layout_height="match_parent"
        android:background="@drawable/selector_button_grey"
        android:clickable="true"
        android:padding="5dp">
    
    <ImageView
        android:id="@+id/image_selector"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:background="@color/black"
        android:src="@drawable/Nachos"
         />
    
    <TextView
        android:id="@+id/image_selector_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/image_selector"
        android:gravity="center_horizontal|center_vertical"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:textSize="18sp" />
    </RelativeLayout>
    
        3
  •  0
  •   KeLiuyue    8 年前

    android:scaleType 属性设置为您的ImageView。

    如果要按比例显示图片,可以使用 centerCrop .

    fitXY .

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="350dp"
                    android:layout_height="wrap_content"
                    android:background="@drawable/selector_button_grey"
                    android:clickable="true"
                    android:padding="5dp">
    
        <ImageView
            android:id="@+id/image_selector"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:background="@color/black"
            android:scaleType="fitXY"
            android:src="@drawable/Nachos" />
    
        <TextView
            android:id="@+id/image_selector_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/image_selector"
            android:gravity="center_horizontal|center_vertical"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:textSize="18sp"/>
    
    </RelativeLayout>
    
        4
  •  0
  •   P. Jaiswal    8 年前

    希望这有帮助! 就我而言,它工作得很好。

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="350dp"
        android:layout_height="match_parent"
        android:background="@drawable/Nachos">
    </RelativeLayout>
    
        5
  •  0
  •   Xenolion ariochdivij666    8 年前

    有一些填充设置在 RelativeLayout ImageView 不接触布局中的侧面 相对布局

    android:padding="5dp"
    

    如果你真的想在顶部和底部设置一些填充物,但是 不是 添加 这些:

    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    

    添加 他们

        6
  •  0
  •   Mariano L    8 年前

       BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmapOptions.inJustDecodeBounds = true;
                    try {
                        BitmapFactory.decodeStream(new FileInputStream(f), null, bitmapOptions);
                        BitmapFactory.decodeFile(f.getName(), bitmapOptions);
                        float scale = (bitmapOptions.outHeight * 1f) / (bitmapOptions.outWidth);
                        cache.put(image, value);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
    

    然后使用该值

        imageView.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(IMAGE_SIZE, Math.round(IMAGE_SIZE * imageData.getScale())));
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setAdjustViewBounds(true);
        imageView.setCropToPadding(true);