我遇到显示正确尺寸的ImageViews的问题。我读过的几乎所有帖子都涉及在布局文件中创建的ImageView。但是就我而言,我是以编程方式创建ImageView。
我正在尝试显示位于存储中的特定文件夹中的所有图像。检索到的位图将放置在GridView中的ImageViews中。
这是我的代码:
//pass an array containing all images paths to adapter
imageGrid.setAdapter(new GridViewAdapter(getActivity(), FilePathStrings));
GridVewAdapter代码的一部分:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(5, 5, 5, 5);
imageView.setAdjustViewBounds(true);
}else{
imageView = (ImageView) convertView;
}
imageView.setTag(filepath[position]);
new LoadImages(imageView).execute();
return imageView;
}
用于检索位图的AsyncTask:
private class LoadImages extends AsyncTask<Object, Void, Bitmap> {
private ImageView imgView;
private String path;
private LoadImages(ImageView imgView) {
this.imgView = imgView;
this.path = imgView.getTag().toString();
}
@Override
protected Bitmap doInBackground(Object... params) {
Bitmap bitmap = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = 6;
bitmap = BitmapFactory.decodeFile(path, bmOptions);
try {
int dimension = getSquareCropDimensionForBitmap(bitmap);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
}catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if (!imgView.getTag().toString().equals(path)) {
return;
}
if(result != null && imgView != null){
imgView.setVisibility(View.VISIBLE);
imgView.setImageBitmap(result);
}else{
imgView.setVisibility(View.GONE);
}
}
}
获得相同尺寸的方法使位图显示为正方形:
private int getSquareCropDimensionForBitmap(Bitmap bitmap) {
int dimension;
//If the bitmap is wider than it is height then use the height as the square crop dimension
if (bitmap.getWidth() >= bitmap.getHeight()) {
dimension = bitmap.getHeight();
}
//If the bitmap is taller than it is width use the width as the square crop dimension
else {
dimension = bitmap.getWidth();
}
return dimension;
}
布局文件中的GridView:
<GridView
android:id="@+id/imageGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="30dp"
android:numColumns="5"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="30dp">
</GridView>
我得到的结果:
带圆圈的细线“实际上是显示的ImageView。”
非常感谢任何帮助。提前谢谢!
答案 0 :(得分:0)
<GridView
android:id="@+id/imageGridView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="30dp">
答案 1 :(得分:0)
我在GridView上面有其他视图,因此我无法在.xml文件中将其高度设置为fill_parent / match_parent,因为它会影响上面的视图。
我的问题的解决方案是以编程方式设置GridView的高度和宽度。
替换:
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
有了这个:
imageView.setLayoutParams(new GridView.LayoutParams(int height, int width));
说明:
任何有更好答案的人,请发布:)