GridView kéo dài ImageView không đồng nhất (GridView Stretching ImageView Non-Uniformly)


問題描述

GridView kéo dài ImageView không đồng nhất (GridView Stretching ImageView Non‑Uniformly)

I have a GridView that I am using to display a row of 4 images, each image is 200 x 200 pixels. Here is the layout xml:

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/myGridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:numColumns="4" >
    </GridView>

</RelativeLayout>

When I run my app here is what the images look like:

As you can see the images are getting scaled (narrowed) to fit the screen. 

How do I make it so the images are stretched both vertically and horizontally to maintain the correct aspect ratio?

Note that I am using an adapter to draw the images:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(context);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setBackgroundResource(imageIds[position]);
    return imageView;
}

參考解法

方法 1:

I got the same issue in last weak.Use my image view class and solve this issue.

public class iv_autoSizedImageView extends ImageView {

    public iv_autoSizedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        // TODO Auto‑generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        try {

            getLayoutParams().height = getMeasuredWidth();

        } catch (Exception e) {
            // TODO: handle exception

        }

    }

}
  

layout.xml

 <com.sample.test.iv_autoSizedImageView
            android:id="@+id/IV_NEWS_IMAGE"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

(by Jan TacciXEENA)

參考文件

  1. GridView Stretching ImageView Non‑Uniformly (CC BY‑SA 3.0/4.0)

#imageview #gridview #Android #stretch






相關問題

Android imageview 未在 TableRow 中顯示 (Android imageview not displaying in TableRow)

Android中的手勢 (Gestures in Android)

GridView kéo dài ImageView không đồng nhất (GridView Stretching ImageView Non-Uniformly)

ios在表格視圖單元格中視覺調整圖像大小 (ios visually resize image in a table view cell)

IndoorAtlas SDK 2.0:使用 Picasso 和自定義 ImageView (IndoorAtlas SDK 2.0: Using Picasso with custom ImageView)

當圖像的寬度小於顯示器的寬度時,如何在視圖尋呼機內對齊圖像視圖的中心? (How to align center an image view inside a view pager when the image's width is less than the display's?)

正確旋轉 ImageView 和 FrameLayout (Rotate ImageView and FrameLayout correctly)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

Android:擴展 onTouchEvent 以進行拖動的 ImageView (Android: ImageView that extends onTouchEvent for Drag)

將旋轉的文本放在圖像視圖上 (Put rotated text on an imageview)

如何在底部設置圖像視圖 (how to set imageview in bottom)

我如何使用數組來顯示匹配的 Imageview? (How Could I use array to show match Imageview?)







留言討論