public class ImageAdapter extends BaseAdapter {
private Context mContext;
private List<String> photsStringList;
private ImageView mImageView;
public HashMap<Integer,Bitmap> bitmapCache;
public int maxCachePosition;
public ImageAdapter(Context context, List<String> list) {
mContext = context;
photsStringList = list;
bitmapCache=new HashMap<Integer,Bitmap>();
initBitmap();
}
public void initBitmap(){
if(photsStringList!=null&&photsStringList.size()!=0){
int start = ImageSave.galleryPotion-2;
if(start<=0){
start = 0;
}
int end = ImageSave.galleryPotion+2;
if(end>=photsStringList.size()){
end = photsStringList.size();
}
for(int i = start;i<end;i++){
Bitmap bitmap = getBitmap(photsStringList.get(i));
bitmapCache.put(i, bitmap);
}
}
}
public Bitmap getBitmap(String path){
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inJustDecodeBounds = false;
File f = new File(path);
if (f.length() < 1024 * 80){
options.inSampleSize = 1;
}else if (f.length() < 1024 * 240){
options.inSampleSize = 2;
} else if (f.length() < 1024 * 465){
options.inSampleSize = 3;
} else if (f.length() < 1024 * 880){
options.inSampleSize = 4;
} else if (f.length() < 1024 * 1440) {
options.inSampleSize = 5;
} else if (f.length() < 1024 * 2560) {
options.inSampleSize = 6;
} else {
options.inSampleSize = 8;
}
bm = BitmapFactory.decodeFile(path,options);
return bm;
}
public int getCount() {
return photsStringList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public void deleteImage(int i){
photsStringList.remove(i);
bitmapCache.remove(i);
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
mImageView = new ImageView(mContext);
if(ImageSave.isMove==true){
if(position!=photsStringList.size()){
if(position>ImageSave.createPosition){
int indexStart1 = position+3;
if(photsStringList!=null&&photsStringList.size()!=0){
if(indexStart1>=photsStringList.size()){
indexStart1 = photsStringList.size()-1;
}
}
Bitmap b1 = bitmapCache.get(indexStart1);
// Bitmap b2 = bitmapCache.get()
if(b1==null){
CreateBitmapCache cb = new CreateBitmapCache(position, photsStringList, bitmapCache, ImageSave.MOVE_RIGHT);
Thread createImageCacheThread = new Thread(cb);
createImageCacheThread.start();
}
}
}
if(position!=0){
if(position<ImageSave.createPosition){
int indexStart2 = position -3;
if(indexStart2<=0){
indexStart2 = 0;
}
Bitmap b1 = bitmapCache.get(indexStart2);
if(b1==null){
CreateBitmapCache cb = new CreateBitmapCache(position, photsStringList, bitmapCache, ImageSave.MOVE_LEFT);
Thread createImageCacheThread = new Thread(cb);
createImageCacheThread.start();
}
}
}
}
ImageSave.createPosition = position;
Bitmap bm = bitmapCache.get(position);
if(bm!=null){
mImageView.setImageBitmap(bm);
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
if(bm!=null){
System.out.println("bm.getheight"+bm.getHeight());
System.out.println("bm.getwidth"+bm.getWidth());
if ((float) bm.getHeight() / bm.getWidth() <= 1.5){
mImageView.setLayoutParams(new Gallery.LayoutParams(BrowsePic.screen_width,(int)((float) bm.getHeight() / bm.getWidth() * BrowsePic.screen_width)));
}else{
mImageView.setLayoutParams(new Gallery.LayoutParams((int) (BrowsePic.screen_height * (float) bm.getWidth() / bm.getHeight()), BrowsePic.screen_height));
}
}
}else{
bm = getBitmap(photsStringList.get(position));
mImageView.setImageBitmap(bm);
bitmapCache.put(position, bm);
if(bm!=null){
if ((float) bm.getHeight() / bm.getWidth() <= 1.5){
mImageView.setLayoutParams(new Gallery.LayoutParams(BrowsePic.screen_width,(int)((float) bm.getHeight() / bm.getWidth() * BrowsePic.screen_width)));
}else{
mImageView.setLayoutParams(new Gallery.LayoutParams((int) (BrowsePic.screen_height * (float) bm.getWidth() / bm.getHeight()), BrowsePic.screen_height));
}
}
}
System.gc();
return mImageView;
}
}
android自定义 BaseAdapter
最新推荐文章于 2024-10-15 14:56:18 发布