gpt4 book ai didi

android - 使用 picasso 的图像加载问题

转载 作者:行者123 更新时间:2023-11-30 01:27:02 24 4
gpt4 key购买 nike

我有一个微调器。当我在微调器中设置图像时。加载图像不正确。所以发生了什么事一些图像看起来很小,一些图像不显示,当我向下或向上滚动时一些图像不显示。有时加载时发生了什么图片需要时间

SpinnerAdapterRecharge.java

public class SpinnerAdapterRecharge extends ArrayAdapter<listview_data> {
private Activity context;
listview_data data[] = null;
int layoutResourceId;

public SpinnerAdapterRecharge(Activity context, int resource,listview_data[] data)
{
super(context, resource, data);
this.context = context;
this.data = data;
this.layoutResourceId = resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return getDropDownView(position, convertView, parent);
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // This view starts when we click the spinner.
View row = convertView;
listHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new listHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.spinnerimageIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.spinnerOperator);

row.setTag(holder);
}
else
{
holder = (listHolder)row.getTag();
}
listview_data item = data[position];

if(position == 0)
{
holder.imgIcon.setVisibility(View.GONE);
holder.txtTitle.setText(context.getResources().getString(R.string.lbl_operator));
}
else {
Picasso.with(context)
.load(item.icon)
.resize(30, 30)
.error(R.drawable.imagenotavailable)
.into(holder.imgIcon);

holder.txtTitle.setText(item.title);
}

return row;
}
static class listHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}

最佳答案

为了回答您的问题:

现在 Picasso 异步加载图像:

 Picasso.with(context)
.load(item.icon)
.resize(30, 30)
.error(R.drawable.imagenotavailable)
.into(holder.imgIcon);

如果想同步拥有,可以使用Picasso的get()方法。

 Bitmap bitmap = Picasso.with(context)
.load(item.icon)
.resize(30, 30)
.error(R.drawable.imagenotavailable)
.get();
someImageView.setImageBitmap(bitmap);

这可能是来自另一个 SO 答案的更好的解决方案。

/**
* Loads the request into an imageview.
* If called from a background thread, the request will be performed synchronously.
* @param requestCreator A request creator
* @param imageView The target imageview
* @param callback a Picasso callback
*/
public static void into(RequestCreator requestCreator, ImageView imageView, Callback callback) {
boolean mainThread = Looper.myLooper() == Looper.getMainLooper();
if (mainThread) {
requestCreator.into(imageView, callback);
} else {
try {
Bitmap bitmap = requestCreator.get();
imageView.setImageBitmap(bitmap);
if (callback != null) {
callback.onSuccess();
}
} catch (IOException e) {
if (callback != null) {
callback.onError();
}
}
}
}

来源:Synchronous image loading on a background thread with Picasso - without .get()


基本上,这就是同步加载的要点。

关于android - 使用 picasso 的图像加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36304003/

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