在GridView中修复微小图像? [英] Fixing tiny images in GridView?
问题描述
我正在尝试按照Android开发人员指南/教程实现基本的GridView库。
I'm trying to implement a basic GridView gallery as per the Android Developer Guide/Tutorial.
我的网格中的ImageViews是从用户的相机中获取的位图。
The ImageViews inside my grid are Bitmaps taken from a user's camera.
除了我的图像非常小这一事实外,这种方法很好。
This works fine except for the fact that my images are incredibly small.
我的xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
我的 ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Bitmap> imgs;
public ImageAdapter(Context c, ArrayList<Bitmap> arrayList) {
mContext = c;
imgs = arrayList;
}
public int getCount() {
return imgs.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(imgs.get(position));
return imageView;
}
}
我尝试了各种各样的事情修复此问题,但唯一似乎有效的是将ImageView的布局参数调整为大量数字(例如 GridView.LayoutParams(300,300)
)。
然而,即使我这样做,我的图像仍然相对较小(即无处接近300dp x 300dp)。
I've tried various things to fix this issue, but the only one that seems to work is adjusting the ImageView's layout params to enormous numbers (e.g. GridView.LayoutParams(300, 300)
).
However, even if I do this, my images are still relatively small (i.e. nowhere near 300dp x 300dp).
知道我做错了什么?
推荐答案
将我的适配器中的布局参数设置为 GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ))
设法解决问题。
Setting layout params in my adapter to GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
managed to fix the issue.
这篇关于在GridView中修复微小图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!