有没有一种方法可以在列表视图上显示项目后立即将可绘制对象加载到 Imageview
is there a way to load drawable to Imageview as soon as item shown on listview
使用大量本地图像滚动 网格视图 使我的应用程序在具有少量 RAM 并显示消息 java.lang.OutOfMemoryError
的设备或至少在性能良好的设备中崩溃滚动非常勉强并且有滞后。我努力寻找解决方案,发现远程图像存在一些很好的解决方案,例如:Universal-Image-Loader,它从 Web 加载图像并将它们兑现并管理滚动。但我不能在我的案例中使用它。
这是我加载图像的适配器:
public class MyAdapter extends ArrayAdapter<StructureCase> {
private LayoutInflater mInflater = null;
public Context context;
public Class distinationActivity = null;
public MyAdapter(Context context, int textViewResourceId, List<StructureCase> objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
// mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
public ImageView gem_img = null;
public TextView gem_name = null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final View v;
final StructureCase item = getItem(position);
if (convertView == null) {
convertView = this.mInflater.inflate(R.layout.my_grid_list, null);
//mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.my_grid_list, parent, false);
viewHolder = new ViewHolder();
convertView.setTag(viewHolder);
viewHolder.gem_img = (ImageView) convertView.findViewById(R.id.imageView_mygrid_list);
viewHolder.gem_name = (TextView) convertView.findViewById(R.id.textView_mygrid_list);
viewHolder.gem_name.setTypeface(G.typeFacePrs);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.gem_name.setText(item.g_name);
int id = G.currentActivity.getResources().getIdentifier(item.g_image+"_tmb", "drawable", G.currentActivity.getPackageName());
if(id != 0){
Drawable drawable = G.currentActivity.getResources().getDrawable(id);
viewHolder.gem_img.setImageDrawable(drawable);
}else{
viewHolder.gem_img.setImageResource(R.drawable.gem1);
}
viewHolder.gem_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(G.currentActivity, GemDetaileActivity.class);
i.putExtra("id", item.g_ID);
i.putExtra("content", item.g_text);
i.putExtra("name", item.g_name);
i.putExtra("name_en", item.g_name_en);
i.putExtra("image", item.g_image);
i.putExtra("image_count", item.g_image_count);
i.putExtra("hadis", item.g_hadis);
i.putExtra("good_for", item.g_good_for);
G.currentActivity.startActivity(i);
G.currentActivity.overridePendingTransition(R.anim.in_left_2_right, R.anim.out_left_2_right);
}
});
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
}
我认为我的问题是预加载所有图像,如果有一种方法可以在滚动显示它的项目后立即将 Drawable 加载到图像视图,我的问题将解决。
终于在一个有Picasso的朋友的帮助下解决了这个问题。它是
Android 的强大图像下载和缓存库。我知道,但我不知道它也可以在本地资源上使用。
您必须下载最后一个 jar 文件并将其添加到您的项目依赖项中。添加构建 gradle 像这样:
compile 'com.squareup.picasso:picasso:2.4.2'
然后您可以通过以下方式使用它:
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
这里是 Picasso 的 link。
使用大量本地图像滚动 网格视图 使我的应用程序在具有少量 RAM 并显示消息 java.lang.OutOfMemoryError
的设备或至少在性能良好的设备中崩溃滚动非常勉强并且有滞后。我努力寻找解决方案,发现远程图像存在一些很好的解决方案,例如:Universal-Image-Loader,它从 Web 加载图像并将它们兑现并管理滚动。但我不能在我的案例中使用它。
这是我加载图像的适配器:
public class MyAdapter extends ArrayAdapter<StructureCase> {
private LayoutInflater mInflater = null;
public Context context;
public Class distinationActivity = null;
public MyAdapter(Context context, int textViewResourceId, List<StructureCase> objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
// mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
public ImageView gem_img = null;
public TextView gem_name = null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final View v;
final StructureCase item = getItem(position);
if (convertView == null) {
convertView = this.mInflater.inflate(R.layout.my_grid_list, null);
//mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.my_grid_list, parent, false);
viewHolder = new ViewHolder();
convertView.setTag(viewHolder);
viewHolder.gem_img = (ImageView) convertView.findViewById(R.id.imageView_mygrid_list);
viewHolder.gem_name = (TextView) convertView.findViewById(R.id.textView_mygrid_list);
viewHolder.gem_name.setTypeface(G.typeFacePrs);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.gem_name.setText(item.g_name);
int id = G.currentActivity.getResources().getIdentifier(item.g_image+"_tmb", "drawable", G.currentActivity.getPackageName());
if(id != 0){
Drawable drawable = G.currentActivity.getResources().getDrawable(id);
viewHolder.gem_img.setImageDrawable(drawable);
}else{
viewHolder.gem_img.setImageResource(R.drawable.gem1);
}
viewHolder.gem_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(G.currentActivity, GemDetaileActivity.class);
i.putExtra("id", item.g_ID);
i.putExtra("content", item.g_text);
i.putExtra("name", item.g_name);
i.putExtra("name_en", item.g_name_en);
i.putExtra("image", item.g_image);
i.putExtra("image_count", item.g_image_count);
i.putExtra("hadis", item.g_hadis);
i.putExtra("good_for", item.g_good_for);
G.currentActivity.startActivity(i);
G.currentActivity.overridePendingTransition(R.anim.in_left_2_right, R.anim.out_left_2_right);
}
});
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
}
我认为我的问题是预加载所有图像,如果有一种方法可以在滚动显示它的项目后立即将 Drawable 加载到图像视图,我的问题将解决。
终于在一个有Picasso的朋友的帮助下解决了这个问题。它是 Android 的强大图像下载和缓存库。我知道,但我不知道它也可以在本地资源上使用。
您必须下载最后一个 jar 文件并将其添加到您的项目依赖项中。添加构建 gradle 像这样:
compile 'com.squareup.picasso:picasso:2.4.2'
然后您可以通过以下方式使用它:
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
这里是 Picasso 的 link。