java - Loading bitmaps in an other thread without get new memory addresses -
i'm trying load in images disk in other thread not slow downwards draw thread problem when object hold bitmap transfered new thread have different pointers. i'm not sure how java handles pointers across multipliable threads i'm looking solution have both threads working same object (shallow copy) , not depth re-create of object between threads.
here code
new loadimagetask().execute(thistimedrawbitmaps.indexof(bitmapwrapper), gameengine); private class loadimagetask extends asynctask { protected bitmapwrapper loadimage(bitmapwrapper bitmapwrapper, gameengine gameengine) { //loading image in temp object avoid fatal error 11 bitmapfactory.options options = new bitmapfactory.options(); options.inscaled = true; bitmap tempimage = bitmapfactory.decoderesource(sceptrum.getresources, bitmapwrapper.getid(), options); tempimage = bitmap.createscaledbitmap(bitmapwrapper.getbitmap(), (int) (bitmapwrapper.getbitmap().getwidth() * gameengine.getscale()), (int) (bitmapwrapper.getbitmap().getheight() * gameengine.getscale()), false); //so image can gc. bitmap removepointer = bitmapwrapper.getbitmap(); //to avoid fatal error 11 bitmapwrapper.setbitmap(tempimage); //removing old image. removepointer.recycle(); homecoming bitmapwrapper; } @override /** * add together bitmapwrapper want load , create sure add together gameengine lastly parameter. */ protected object doinbackground(object... params) { (int = 0; < params.length - 1; i++) { homecoming loadimage(thistimedrawbitmaps.get((integer) params[i]), (gameengine) params[params.length - 1]); } homecoming null; } } public class bitmapwrapper{ private bitmap bitmap; /** * utilize send bitmap parameter. modify or read info to/from bitmap utilize methods provided bitmapwrapper. * @return */ public bitmap getbitmap() { homecoming bitmap; } public void setbitmap(bitmap bitmap) { this.bitmap = bitmap; } private int id; public int getid() { homecoming id; } private point originalsize; public point getoriginalsize() { homecoming originalsize; } public bitmapwrapper(bitmap bitmap, int id) { this.setbitmap(bitmap); id = id; originalsize = new point(bitmap.getwidth(), bitmap.getheight()); } public int getwidth() { homecoming bitmap.getwidth(); } public int getheight() { homecoming bitmap.getheight(); } public void createscaledbitmap(gameengine gameengine) { bitmap = bitmap.createscaledbitmap(bitmap, (int) (originalsize.x * gameengine.getscale()), (int) (originalsize.y * gameengine.getscale()), false); } public void createbitmap() { bitmapfactory.options options = new bitmapfactory.options(); options.inscaled = false; bitmap = bitmapfactory.decoderesource(sceptrum.getresources, id, options); } }
this not pointer problem (there rearely such problems in java:) ). if want load images in background thread should create (static) asynctask, holds weak reference views in layout, want bitmaps placed. in doinbackground should load images , homecoming them. in postexecute (which called on ui thread) receive result of doinbackground (the bitmaps). after checking if referenced views still there (activity still alive) can utilize bitmaps (place them in views).
you cannot modify ui elements other threads, except ui thread (this holds true ui framework).
java android multithreading bitmap
No comments:
Post a Comment