android - Loading Bitmaps Efficiently - showing solid color on start -
i have used loading bitmaps efficiently developer's guide , followed instructions point point tutorial.
as may know, code used:
public static int calculateinsamplesize(bitmapfactory.options options, int reqwidth,int reqheight){ //raw height , width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height>reqheight || width>reqwidth) { final int heightratio = math.round((float)height / (float)reqheight); final int widthratio = math.round((float)width / (float)reqwidth); insamplesize = heightratio < widthratio ? heightratio : widthratio; } return insamplesize; } public static bitmap decodesampledbitmapfromresource(resources res, int resid,int reqwidth,int reqheight){ //first decode injustdecodebounds = true check dimensions. final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decoderesource(res, resid, options); //calculate insamplesize options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); //decode bitmap insamplesize options.injustdecodebounds = false; return bitmapfactory.decoderesource(res, resid, options); }
and calling method in switch case as:
handler.postdelayed(new runnable() { @override public void run() { // todo auto-generated method stub learn_full_iv.setvisibility(view.visible); learn_full_iv.startanimation(anim); switch (id) { case r.id.a: //learn_full_iv.setimageresource(r.drawable.aeroplane); learn_full_iv.setimagebitmap(decodesampledbitmapfromresource( getresources(), r.drawable.aeroplane, learn_full_iv.getwidth(), learn_full_iv.getwidth())); toast.maketext(getapplicationcontext(), "width : " + learn_full_iv.getwidth() + "height : " + learn_full_iv.getheight(), toast.length_short).show(); playsound(1, 2); break; case r.id.b: //learn_full_iv.setimageresource(r.drawable.ball); learn_full_iv.setimagebitmap(decodesampledbitmapfromresource( getresources(), r.drawable.ball, learn_full_iv.getwidth(), learn_full_iv.getwidth())); toast.maketext(getapplicationcontext(), "width : " + learn_full_iv.getwidth() + "height : " + learn_full_iv.getheight(), toast.length_short).show(); playsound(3, 4); break; case r.id.c: //learn_full_iv.setimageresource(r.drawable.camel); learn_full_iv.setimagebitmap(decodesampledbitmapfromresource( getresources(), r.drawable.camel, learn_full_iv.getwidth(), learn_full_iv.getwidth())); toast.maketext(getapplicationcontext(), "width : " + learn_full_iv.getwidth() + "height : " + learn_full_iv.getheight(), toast.length_short).show(); playsound(5, 6); break; case r.id.d: //learn_full_iv.setimageresource(r.drawable.drum); learn_full_iv.setimagebitmap(decodesampledbitmapfromresource( getresources(), r.drawable.drum, learn_full_iv.getwidth(), learn_full_iv.getwidth())); toast.maketext(getapplicationcontext(), "width : " + learn_full_iv.getwidth() + "height : " + learn_full_iv.getheight(), toast.length_short).show(); playsound(7, 8); break; default: break; }
and problem every time on loading oncreate() , image first clicked display solid color of image , not whole image, shown below:
and next click, whether on same image or next image, code works fine as:
and else works fine until activity restarted. if restart or resume activity same thing happening.
so, going wrong?
i solved & figured out self , small on android loading of images:
@override public void run() { // todo auto-generated method stub learn_full_iv.setvisibility(view.visible); learn_full_iv.startanimation(anim); switch (id) { case r.id.a: //learn_full_iv.setimageresource(r.drawable.aeroplane); learn_full_iv.setimagebitmap(decodesampledbitmapfromresource( getresources(), r.drawable.aeroplane, learn_full_iv.getwidth(), learn_full_iv.getwidth())); toast.maketext(getapplicationcontext(), "width : " + learn_full_iv.getwidth() + "height : " + learn_full_iv.getheight(), toast.length_short).show(); playsound(1, 2); break; ...........
the problem in above code enabling imageview "learn_full_iv" @ start of run method have width & height "0" default image still not set on imageview
and if calling :
learn_full_iv.setimagebitmap(decodesampledbitmapfromresource( getresources(), r.drawable.aeroplane, learn_full_iv.getwidth(), learn_full_iv.getwidth()));
the width & height 0 in case so, asolid color image displayed.so , used method width & height of screen integer values & passed them corresponding width & height sections in above bitmap decode method.
thats , having width & height values , th bitmap shown now.
simple!!!
Comments
Post a Comment