在网格视图中设置图像视图高度和宽度

时间:2017-08-29 02:18:56

标签: android

我想在我的应用中创建一个图片库

我使用网格视图显示图像,但显示图像的图像

我如何才能显示相同的图像?例如其他图库应用

布局文件:

<GridView
        android:id="@+id/galleryGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:rowCount="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="0dp"
        android:horizontalSpacing="0dp" />

java文件:

public class GalleryManager extends BaseAdapter {
    Context context;
    List<Drawable> list;

    public GalleryManager(Context context , List<Drawable> list ) {
        this.context = context;
        //this.list = list;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            ImageView imageView = new ImageView(context);
            imageView.setImageDrawable(list.get(position));
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            return imageView;
        }
            catch(Exception e){
                Log.i("Tag", "getView: "+e.toString());
            }
            return null;
    }
}

2 个答案:

答案 0 :(得分:2)

尝试

public class MyImageView extends android.support.v7.widget.AppCompatImageView {
public MyImageView(Context context) {
    super(context);
}

public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   // super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = getContext().getResources().getDimensionPixelSize(R.dimen.space);
    setMeasuredDimension(size, size);
}
}

在dimen.xml中

<dimen name="space">100dp</dimen>

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    try {
        MyImageView imageView = new MyImageView(context);
        imageView.setImageDrawable(list.get(position));
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        return imageView;
    }
        catch(Exception e){
            Log.i("Tag", "getView: "+e.toString());
        }
        return null;
}
}

答案 1 :(得分:0)

尝试在java代码中将图像权重设置为1