chi*_*hip 1 android listview palette picasso
我有一个ListView,我正在玩Palette支持库.当使用BitmapFactory.decodeStream从URL生成位图时,这会引发异常(网络在UI线程上)或可能非常昂贵.我如何使这个异步?我想不出任何有效的方法来做到这一点.什么是最好的方法?
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_grid, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.img_photo);
holder.bg = (LinearLayout) convertView.findViewById(R.id.bg_title);
holder.text = (TextView) convertView.findViewById(R.id.txt_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(mContext)
.load(mShows.get(position).poster)
.into(holder.image);
try {
URL url = new URL(mShows.get(position).poster);
Bitmap bitmap = BitmapFactory.decodeStream(
url.openConnection().getInputStream()); // Too expensive!!!
Palette palette = Palette.generate(bitmap);
holder.text.setText(mShows.get(position).title);
holder.text.setTextColor(palette.getVibrantColor().getRgb());
holder.bg.setAlpha(0.4f);
holder.bg.setBackgroundColor(palette.getDarkMutedColor().getRgb());
} catch (IOException e) {
e.printStackTrace();
}
return convertView;
}
Run Code Online (Sandbox Code Playgroud)
你可以使用毕加索into(...)与Callback当图像已成功加载参数:
Picasso.with(mContext)
.load(mShows.get(position).poster)
.into(holder.image, new Callback() {
@Override public void onSuccess() {
Bitmap bitmap = ((BitmapDrawable)holder.image.getDrawable()).getBitmap();
// do your processing here....
}
@Override public void onError() {
// reset your views to default colors, etc.
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1258 次 |
| 最近记录: |