gpt4 book ai didi

java - 无法在 ListView 中设置图像

转载 作者:行者123 更新时间:2023-11-30 03:10:55 25 4
gpt4 key购买 nike

我正在尝试实现如下所示的 Pinterest 样式 ImageView :

enter image description here

我希望从我的 SDCard 中的特定文件夹中设置图像。我正在尝试以下代码,但无法从文件夹中获取图像(显示空白屏幕)。

代码:

ItemsActivity.java :

private void loadItems(){
String[] leftItems = new String[]{};
String[] rightItems = new String[]{};
ItemsAdapter mAdapter = null;
ArrayList<String> image_items = ItemsAdapter.getImages();
System.out.println("The images are obtained in image_items");
System.out.println("The list of images are" + image_items.toString());
mAdapter = new ItemsAdapter(this,R.layout.item,image_items);
listViewLeft.setAdapter(mAdapter);
listViewRight.setAdapter(mAdapter);
leftViewsHeights = new int[leftItems.length];
rightViewsHeights = new int[rightItems.length];
}

项目适配器.java :

public class ItemsAdapter extends ArrayAdapter<String>{

Context context;
LayoutInflater inflater;
int layoutResourceId;
float imageWidth;
InputStream is;
int item;
public ItemsAdapter(Context context, int layoutResourceId, ArrayList<String> items) {
super(context, layoutResourceId, items);
this.context = context;
this.layoutResourceId = layoutResourceId;
float width = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
float margin = (int)convertDpToPixel(10f, (Activity)context);
imageWidth = ((width - (3 * margin)) / 2);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout row = (FrameLayout) convertView;
ItemHolder holder;
String item = getItem(position);
if (row == null) {
holder = new ItemHolder();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (FrameLayout) inflater.inflate(layoutResourceId, parent, false);
ImageView itemImage = (ImageView)row.findViewById(R.id.item_image);
holder.itemImage = itemImage;
} else {
holder = (ItemHolder) row.getTag();
}
row.setTag(holder);
setImageBit(item, holder.itemImage);
return row;
}

public static class ItemHolder
{
ImageView itemImage;
}

// resize the image proportionately so it fits the entire space
private void setImageBit(String path, ImageView imageView){
path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
Bitmap bitmap = BitmapFactory.decodeFile(path);
float i = ((float) imageWidth) / ((float) bitmap.getWidth());
float imageHeight = i * (bitmap.getHeight());
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
params.height = (int) imageHeight;
params.width = (int) imageWidth;
imageView.setLayoutParams(params);
int count = 10;
String file_name = null;
for(int j = 1;j<count;j++)
{
file_name = "Image " + i + ".jpg";
}
//ArrayList<String> img_bitmap;
//Bitmap bmp = ImgBitFromFile(path, file_name);
imageView.setImageBitmap(bitmap);

}

public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
public static ArrayList<String> getImages()
{
String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
File mainfolder = new File(path);
ArrayList<String> files = new ArrayList<String>();
if(mainfolder.listFiles() == null)
return null;
for(File f : mainfolder.listFiles())
{
if(f.getName().contains(".jpg"))
{
files.add(f.getAbsolutePath());
}
}
return files;
}

我可以清楚地看到 ItemsAdapter.java 中的 getImages() 方法检索了所有图像(我已经在 logcat 中打印了这些图像)但它似乎没有在适配器中设置图像。我的 logcat 如下:

01-08 17:30:35.010: E/Trace(14821): error opening trace file: No such file or directory (2)
01-08 17:30:35.040: V/ActivityThread(14821): Class path: /data/app/com.vladimir.pinterestlistview-2.apk, JNI path: /data/data/com.vladimir.pinterestlistview/lib
01-08 17:30:35.100: I/System.out(14821): The images are got in image_items
01-08 17:30:35.100: I/System.out(14821): The list of images are[/storage/sdcard0/Pictures/SanPics2/Image 7.jpg, /storage/sdcard0/Pictures/SanPics2/Image 1.jpg, /storage/sdcard0/Pictures/SanPics2/Image 2.jpg, /storage/sdcard0/Pictures/SanPics2/Image 3.jpg, /storage/sdcard0/Pictures/SanPics2/Image 4.jpg, /storage/sdcard0/Pictures/SanPics2/Image 5.jpg, /storage/sdcard0/Pictures/SanPics2/Image 8.jpg, /storage/sdcard0/Pictures/SanPics2/1388143033275.jpg, /storage/sdcard0/Pictures/SanPics2/1388143591263.jpg, /storage/sdcard0/Pictures/SanPics2/1388143312172.jpg, /storage/sdcard0/Pictures/SanPics2/1388143530089.jpg, /storage/sdcard0/Pictures/SanPics2/Image 6.jpg]
01-08 17:30:35.230: I/Adreno200-EGL(14821): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3_RB3.04.01.01.42.002_msm8625_JB_REL_2.0.3_Merge_release_AU (Merge)
01-08 17:30:35.230: I/Adreno200-EGL(14821): Build Date: 12/12/12 Wed
01-08 17:30:35.230: I/Adreno200-EGL(14821): Local Branch:
01-08 17:30:35.230: I/Adreno200-EGL(14821): Remote Branch: m/jb_rel_2.0.3
01-08 17:30:35.230: I/Adreno200-EGL(14821): Local Patches: NONE
01-08 17:30:35.230: I/Adreno200-EGL(14821): Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_2.0.3_RB3.04.01.01.42.002 + NOTHING
01-08 17:30:35.310: E/ViewRootImpl(14821): performLayout : host is null
01-08 17:30:35.330: E/ViewRootImpl(14821): performLayout : host is null
01-08 17:30:35.340: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(0, 0 - 480, 800)
01-08 17:30:40.620: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(0, 0 - 480, 800)
01-08 17:30:40.840: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:41.040: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:41.250: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:41.450: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:41.670: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:41.920: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:42.220: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:42.660: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:43.280: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:43.860: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:44.390: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:44.690: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)
01-08 17:30:45.090: D/HardwareRenderer(14821): draw surface is valid dirty= Rect(15, 110 - 240, 800)

当我触摸屏幕时,log cat 中的这个绘制语句会重复出现。我不确定我错过了什么;无论是大事还是傻事。如何将我的摄像头图像设置为上述样式?任何帮助将不胜感激。提前致谢。

P.S : 我使用两个同步 ListView 来显示图像,它们可以同时滚动。

最佳答案

查看 StaggerdGridView这与您的需要相同,而且易于实现。

关于java - 无法在 ListView 中设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com