java - 在 gridview 中缩放 imageview 时的空白

标签 java android xml gridview

我希望实现类似于 Google Play 卡片的用户界面。

我已经创建了一个 GridView 和卡片的背景作为一个 xml 文件。现在,我正在寻找适合卡上的图像,以便底部阴影可见,但图像的宽度与卡的宽度相同。问题是图像周围有空白区域,即使我将 GridView.LayoutParams 设置为与图像大小成比例。我尝试了不同的 scaleType,但都没有用。此外,一旦图像的宽度适合卡片的宽度,我如何确保底部阴影仍然可见?

enter image description here

我的图像适配器类

package me.zamaaan.wallpaper;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter{
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.background1, R.drawable.background2,
            R.drawable.background3, R.drawable.background4,
            R.drawable.background1, R.drawable.background2,
            R.drawable.background1, R.drawable.background2,
            R.drawable.background3, R.drawable.background4,
            R.drawable.background1, R.drawable.background2,
            R.drawable.background3
    };


    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        int width_dp = 109;
        double height_dp = width_dp/0.5625;
        int width_px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width_dp, mContext.getResources().getDisplayMetrics());

        int height_px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (int)height_dp, mContext.getResources().getDisplayMetrics());

        imageView.setLayoutParams(new GridView.LayoutParams(width_px, height_px));
        imageView.setBackgroundResource(R.drawable.bg_card);

        return imageView;
    }

}

bg_card.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle"
               android:dither="true">

            <corners android:radius="2dp"/>

            <solid android:color="#ccc" />

        </shape>
    </item>

    <item android:bottom="2dp">
        <shape android:shape="rectangle"
               android:dither="true">

            <corners android:radius="2dp" />

            <solid android:color="@android:color/white" />

            <padding android:bottom="8dp"
                     android:left="8dp"
                     android:right="8dp"
                     android:top="8dp" />
        </shape>
    </item>
</layer-list>

gridview.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/landscape"
  android:orientation="horizontal"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:background="#eeeeee"
  android:gravity="center">

<GridView  
    android:id="@+id/grid_view"
    android:layout_marginTop="16.50dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="3"
    android:paddingBottom="8dp"
    android:horizontalSpacing="11dp"
    android:verticalSpacing="7dp"
    android:gravity="center"
    android:stretchMode="columnWidth"
    >

</GridView>

</RelativeLayout>

图片的实际大小是

720x1280px

最佳答案

你的图片没有填满卡片,因为在你的卡片背景中,两边都有 8dp 的内边距

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <item android:bottom="2dp">
        <shape android:shape="rectangle"
               android:dither="true">
            <corners android:radius="2dp" />
            <solid android:color="@android:color/white" />
            <padding android:bottom="8dp"
                     android:left="8dp"
                     android:right="8dp"
                     android:top="8dp" />
        </shape>
    </item>
</layer-list>

移除填充以使其填充宽度。

如果您还想保留阴影,最好为您的背景创建一个 9 色补丁,这样您就可以定义内容所在的位置,例如:

enter image description here

最左边和最上面的黑线代表可缩放区域,最下面和最右边的黑线代表内容所在的区域(注意最右边的内容区域 覆盖阴影区域)。

关于java - 在 gridview 中缩放 imageview 时的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21535806/

相关文章:

XML 属性唯一性

java - 使用 java 处理 Json API 响应

java - 将多个 Maven 项目组合成一场 war

java - 由于 appcompat v7,R.java 没有生成?

Android - 运行时使用 Dagger 创建对象

java - Jaxb 没有发现我的基类的正确儿子

xml - 尝试检测 PL/SQL 过程中的非法 XML 字符

java - SpringBoot : Inject repository into a class defined in service layer

java - 使用 webclient 和 Flux 进行多个异步剩余分页调用

android - 如何在不更新文件的情况下构建文件 Uri?