我有一个应用程序从内部存储中获取图像,并且必须在ImageView中显示它们。 由于图像的加载会对应用程序的性能产生影响,因此我想异步进行。 我提出了这个问题,并被告知使用毕加索。 该应用程序现在很快,但没有显示图像。 这是我用来将图像放入ImageView的代码:
Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);这是适配器,我使用的注释行没有Picasso。 这样,显示了图像,但它延迟了UI:
public class FotoAdapter extends BaseAdapter { private ArrayList<BeanFotos> fotos; private LayoutInflater inflater=null; Context c; Handler handler; public FotoAdapter(Context c, ArrayList<BeanFotos> fotos){ this.fotos=fotos; inflater=LayoutInflater.from(c); this.c=c; } @Override public int getCount() { if(fotos!=null){ return fotos.size(); } return 0; } @Override public Object getItem(int position) { return fotos.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView=inflater.inflate(R.layout.foto_layout, null); holder=new ViewHolder(); holder.lat=(TextView)convertView.findViewById(R.id.textlat); holder.lon=(TextView)convertView.findViewById(R.id.textlon); holder.foto=(ImageView)convertView.findViewById(R.id.foto); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } String path=fotos.get(position).getFotoPath(); Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto); /*Bitmap foto= BitmapFactory.decodeFile(path); int bmWidth=foto.getHeight(); int bmHeight=foto.getHeight(); int ivWidth; int ivHeigth; int new_width; int new_height; ivWidth=dpToPx(80); new_width=ivWidth; new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth)); Bitmap newbitMap = Bitmap.createScaledBitmap(foto, new_width, new_height, true);*/ holder.lat.setText(fotos.get(position).getLatitud().toString()); holder.lon.setText(fotos.get(position).getLongitud().toString()); //holder.foto.setImageBitmap(newbitMap); return convertView; } private int dpToPx(int dp) { float density = c.getResources().getDisplayMetrics().density; return Math.round((float)dp * density); } static class ViewHolder { public TextView lat; public TextView lon; public ImageView foto; } }使用毕加索和展示图像会很棒,但如果不可能,我该怎么办?
谢谢。
I have an app that gets images from internal storage, and must show them in an ImageView. Since the loading of the images has an impact in performance in the app, I would like to do it asynchronously. I made this question, and was told to use Picasso. The app now goes fast, but is not showing the image. This is the code I am using to put the image in the ImageView:
Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);This is the adapter, with the commented lines I used without Picasso. That way, the Image is shown, but it delays the UI:
public class FotoAdapter extends BaseAdapter { private ArrayList<BeanFotos> fotos; private LayoutInflater inflater=null; Context c; Handler handler; public FotoAdapter(Context c, ArrayList<BeanFotos> fotos){ this.fotos=fotos; inflater=LayoutInflater.from(c); this.c=c; } @Override public int getCount() { if(fotos!=null){ return fotos.size(); } return 0; } @Override public Object getItem(int position) { return fotos.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView=inflater.inflate(R.layout.foto_layout, null); holder=new ViewHolder(); holder.lat=(TextView)convertView.findViewById(R.id.textlat); holder.lon=(TextView)convertView.findViewById(R.id.textlon); holder.foto=(ImageView)convertView.findViewById(R.id.foto); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } String path=fotos.get(position).getFotoPath(); Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto); /*Bitmap foto= BitmapFactory.decodeFile(path); int bmWidth=foto.getHeight(); int bmHeight=foto.getHeight(); int ivWidth; int ivHeigth; int new_width; int new_height; ivWidth=dpToPx(80); new_width=ivWidth; new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth)); Bitmap newbitMap = Bitmap.createScaledBitmap(foto, new_width, new_height, true);*/ holder.lat.setText(fotos.get(position).getLatitud().toString()); holder.lon.setText(fotos.get(position).getLongitud().toString()); //holder.foto.setImageBitmap(newbitMap); return convertView; } private int dpToPx(int dp) { float density = c.getResources().getDisplayMetrics().density; return Math.round((float)dp * density); } static class ViewHolder { public TextView lat; public TextView lon; public ImageView foto; } }Would be great to use Picasso and show the Image, but if that is not possible, what could I do?
Thank you.
最满意答案
用于:
从内部存储中获取
Picasso.with(c).load(new File(path)).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);从网址加载
Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);Use this for:
fetching from internal storage
Picasso.with(c).load(new File(path)).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);loading from url
Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);更多推荐
发布评论