android自定义 BaseAdapter

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;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值