作者:lmc的 | 来源:互联网 | 2023-05-19 11:57
ImhavingaproblemofdisplayingImageViewswiththecorrectsize.AlmostallthepoststhatIve
I'm having a problem of displaying ImageViews with the correct size. Almost all the posts that I've read through involves ImageViews that are created in the layout file. However in my case, I'm creating ImageViews programmatically.
我有一个显示正确大小的ImageViews的问题。我读过的几乎所有帖子都涉及在布局文件中创建的ImageView。但是在我的情况下,我是以编程方式创建ImageViews。
I'm trying to display all images from a particular folder located in the storage. The bitmaps retrieved will be placed in ImageViews which are contained within a GridView.
我正在尝试显示位于存储中的特定文件夹中的所有图像。检索到的位图将放置在GridView中,ImageView包含在GridView中。
Here's my code:
这是我的代码:
//pass an array containing all images paths to adapter
imageGrid.setAdapter(new GridViewAdapter(getActivity(), FilePathStrings));
part of GridVewAdapter code:
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 to retrieve bitmaps:
AsyncTask检索位图:
private class LoadImages extends AsyncTask {
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);
}
}
}
Method to get equal dimensions so bitmaps will be displayed as squares:
获得相同尺寸的方法使位图显示为正方形:
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 in the layout file:
布局文件中的GridView:
The result that I've gotten:
结果我得到了:
The circled thin "lines" are actually the ImageViews displayed.
带圆圈的细线“实际上是显示的ImageViews”。
Any help is very much appreciated. Thank you in advance!
很感谢任何形式的帮助。先感谢您!
2 个解决方案