我有一个画廊视图,使用BaseAdapter,我从互联网下载图像,下载图像,并设置为图库异步。大约有2000张图片。 当我旋转屏幕时,asyntask继续,但画廊设置为空,不显示任何现在或旧图像。
这是BaseAdapter的代码:
private class AdaptadorImagenes extends BaseAdapter {
private Context contexto = null;
private ArrayList<Integer> datos = new ArrayList<Integer>();
public AdaptadorImagenes(Context contexto) {
this.contexto = contexto;
}
public void addItem(Integer posicion) {
datos.add(posicion);
notifyDataSetChanged();
}
@Override
public int getCount() {
return datos.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imagen = new ImageView(contexto);
imagen.setImageBitmap(getBitMapRedimensionado(position));
imagen.setScaleType(ScaleType.FIT_XY);
return imagen;
}
/**
* Metodo para redimensionar un bimap
*
* @param posicion
* del bitmap en una lista que contiene los path
* @return bitmap redimensionado
*/
private Bitmap getBitMapRedimensionado(int posicion) {
File fichero = new File(pathImagenes.get(posicion));
Bitmap bmOriginal = redondeaEsquinasBitmap(Bitmap
.createScaledBitmap(
BitmapFactory.decodeFile(fichero.toString()), 210,
210, true));
return bmOriginal;
}
/**
* Metodo para redondear las esquinas de un bitmap
*
* @param bitmap
* a redondear
* @return bitmap redondeado
*/
public Bitmap redondeaEsquinasBitmap(Bitmap bitmap) {
Bitmap salida = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(salida);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return salida;
}
}
在Manifiest中我设置了android:configChanges="keyboardHidden|orientation"
我认为保存一个变量已经下载了多少图像,并且在旋转屏幕时设置了所有图像,但是当超过500时这个过程非常慢。
有什么想法吗?谢谢
答案 0 :(得分:0)
看看ImageLoader。此链接可以帮助Lazy load of images in ListView或http://androidimageloader.com/
此外,您可以在活动中忽略AndroidManifest中的更改方向:
android:name="com.example.MainActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait"