gallery加载大量图片时内存溢出的解决方法(二)
Paint deafalutPaint =newPaint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap,null);
Paint paint =newPaint();
LinearGradient shader =newLinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
returnbitmapWithReflection;
}
}
2、加载图片的线程
classMyThreadextendsThread{
intindex;
publicMyThread(intindex) {
super();
this.index= index;
}
@Override
publicvoidrun() {
if(imageList.get(index).getIsNull().equals(ISTRUE)) {imageList.get(index).setImage(ImageManager.getRefImage(imageList.get(index)
.getPath()));
imageList.get(index).setIsNull(ISFALSE);
}
refreshAdapter();
}
}
3、gallery的滑动事件
privateGallery.OnItemSelectedListeneritemSelected_listener=newGallery.OnItemSelectedListener() {
@Override
publicvoidonItemSelected(AdapterView> arg0, View arg1,intposition,
longarg3) {
toShowIndex= position;
finalHandler handler =newHandler() {
@Override
publicvoidhandleMessage(Message msg) {
if(showingIndex!=toShowIndex) {
showingIndex=toShowIndex;
//业务逻辑处理
if(toShowIndex
addImage(toShowIndex);
}
}
}
};
Thread checkChange =newThread() {
@Override
publicvoidrun() {
intmyIndex =toShowIndex;
try{
sleep(TIME_OUT_DISPLAY);
if(myIndex ==toShowIndex) {
handler.sendEmptyMessage(0);
}
}catch(InterruptedException e) {
e.printStackTrace();
}
}
};
checkChange.start();
TextView gallery_total =(TextView) findViewById(R.id.gallery_total);
gallery_total.setText((toShowIndex+ 1) +"/"+imageList.size());}
@Override
publicvoidonNothingSelected(AdapterView> arg0) {
}
};
4、更新ImageAdapter
privatevoidrefreshAdapter() {
this.runOnUiThread(newRunnable() {
@Override
publicvoidrun() {
adpter.notifyDataSetChanged();
}
});
}
5、从sd卡里获取图片后封装对象
publicList getAllImageMessage() {
List imageList =newArrayList();
getSD(Environment.getExternalStorageDirectory().toString()+"/");
for(inti = 0; i
ImageMessage im =newImageMessage();
im.setPath(pathList.get(i)[0]);
im.setName(pathList.get(i)[1]);
im.setIsNull(ISTRUE);
imageList.add(im);
}
returnimageList;
}