毕加索没有显示图像(Picasso not showing images)

编程入门 行业动态 更新时间:2024-10-10 21:31:25
毕加索没有显示图像(Picasso not showing images)

我有一个应用程序从内部存储中获取图像,并且必须在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);

更多推荐

本文发布于:2023-08-04 17:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419182.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:毕加索   图像   Picasso   showing   images

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!