android - Issue while loading images in SurfaceView -
android - Issue while loading images in SurfaceView -
i need load different images of auto saved in drawable simulate 360 grade rotation, when ever user drag through view have set drag distance 1 time user cross load next frame. using surface view , draw images on canvas. since images big not able load images @ time memory, loading images on fly keeping current image in memory. approach sluggish. can 1 point out doing wrong ? or other improve way accomplish same. help.
this code
/** * @author rajeshcp * class deed top view in view * hierarchy cardetailsactivity * @since 13 feb 2013 */
public class virtualview extends surfaceview implements surfaceholder.callback, ontouchlistener, tweenlistener { private final int frame_width = 1024; private final int frame_height = 462; private interactionlistener minteractionlistener; private final int msnapvelocity = 1000; private velocitytracker mvelocitytracker; protected int mtouchslop; private int mmaxvelocity; private rect mviewarea; private iqtweener mvalueanimator; private colorvariationvo mcolorvariationvo; /**************indicates current frame*********************/ private int mcurrentframeindex; /**********initial touch x coordinate**************/ private int minitialtouchx; /***********the drag distance frame change***********/ private int mdraginterwell = 10; private final int mtotalframes = 36; protected final int mmaxframeskip = 3; /**************identity matrix**********************/ private matrix midentitymatrix; private paint mpaint; /*********the bitmap drawn on canvas*********/ private bitmap mcurrentframe; private viewupdater mviewupdater; /*++++++++++++++++++++++++++++++++++++++++++++++*/ /*+++++++++++++getters , setters++++++++++++++*/ /*++++++++++++++++++++++++++++++++++++++++++++++*/ /** * @param of type null * @return mcolorvariationvo of type colorvariationvo * getter function mcolorvariationvo * @since feb 13, 2013 * @author rajeshcp */ public colorvariationvo getmcolorvariationvo() { homecoming mcolorvariationvo; } /** * @param mcolorvariationvo of type colorvariationvo * @return of type null * setter function mcolorvariationvo * @since feb 13, 2013 * @author rajeshcp */ public void setmcolorvariationvo(colorvariationvo mcolorvariationvo) { this.mcolorvariationvo = mcolorvariationvo; loadnextframe(); } /** * @param context of type context * @return of type virtualview * constructor function * @since feb 13, 2013 * @author rajeshcp */ public virtualview(context context) { super(context); init(); } /** * @param context of type context * @param attrs of type attributeset * @return of type virtualview * constructor function * @since feb 13, 2013 * @author rajeshcp */ public virtualview(context context, attributeset attrs) { super(context, attrs); init(); } /** * @param context of type context * @param attrs of type attributeset * @param defstyle of type int * @return of type virtualview * constructor function * @since feb 13, 2013 * @author rajeshcp */ public virtualview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } /** * @param of type null * @return of type null * function initialize values view * @since 13 feb 2013 */ private void init() { setzorderontop(true); getholder().setformat(pixelformat.transparent); mpaint = new paint(paint.filter_bitmap_flag | paint.dither_flag | paint.anti_alias_flag); midentitymatrix = new matrix(); mcurrentframeindex = 0; mviewupdater = new viewupdater(); minteractionlistener = new interactionlistener(); final viewconfiguration configuration = viewconfiguration.get(getcontext()); mtouchslop = configuration.getscaledtouchslop(); mmaxvelocity = configuration.getscaledmaximumflingvelocity(); getholder().addcallback(this); setontouchlistener(this); setlayertype(view.layer_type_hardware, mpaint); } /** * @param direction of type int * @return of type null * function alter current frame index * @since feb 13, 2013 * @author rajeshcp */ private void initiateframechange(final int direction) { mcurrentframeindex += math.max(-mmaxframeskip, math.min(mmaxframeskip, direction)); if( mcurrentframeindex > (mtotalframes - 1) ) { mcurrentframeindex = (mcurrentframeindex % ( mtotalframes - 1) ); }else if( mcurrentframeindex < 0 ) { mcurrentframeindex = (mtotalframes - 1) + (mcurrentframeindex % (mtotalframes - 1)); } loadnextframe(); } /** * @param index of type int * @return of type bitmap * function create bitmap form * resources * @since feb 13, 2013 * @author rajeshcp */ @suppresslint("usesparsearrays") private bitmap getframe(final int index) { bitmap bitmap = null; if( mcolorvariationvo != null) { final int drawableid = getresourceid(index); seek { bitmap = bitmapfactory.decoderesource(getresources(), drawableid); }catch (exception e) { log.d(getclass().getname(), e.getlocalizedmessage()); } } homecoming bitmap; } /** * @param index of type int * @return of type int * function create resource id * index * @since feb 18, 2013 * @author rajeshcp */ private int getresourceid(final int index) { final string framename = mcolorvariationvo.getmcarassetpath() + "_" + index; int drawableid = 0; seek { @suppresswarnings("rawtypes") class res = r.drawable.class; field field = res.getfield(framename); drawableid = field.getint(null); } grab (exception e) { log.e(getclass().getname(), "failure drawable id.", e); } homecoming drawableid; } /* (non-javadoc) * @see android.view.view#ondraw(android.graphics.canvas) * @since feb 13, 2013 * @author rajeshcp */ @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if( mcurrentframe != null && canvas != null ) { canvas.drawcolor(color.transparent, porterduff.mode.clear); midentitymatrix.reset(); // mpaint.setstyle(paint.style.stroke); // mpaint.setstrokewidth(1); // mpaint.setcolor(color.magenta); // mpaint.settextsize(100); // // canvas.drawtext(string.valueof(mcurrentframeindex), (getwidth() / (mtotalframes - 1) ) * mcurrentframeindex, (getheight() - 100) / 2, mpaint); midentitymatrix.posttranslate(getwidth() - frame_width, (getheight() - frame_height) / 2); canvas.drawbitmap(mcurrentframe, midentitymatrix, mpaint); } } /* (non-javadoc) * @see android.view.surfaceholder.callback#surfacechanged(android.view.surfaceholder, int, int, int) * @since feb 13, 2013 * @author rajeshcp */ @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } /* (non-javadoc) * @see android.view.surfaceholder.callback#surfacecreated(android.view.surfaceholder) * @since feb 13, 2013 * @author rajeshcp */ @override public void surfacecreated(surfaceholder holder) { if( mviewarea == null ) { int top = (getheight() - frame_height) / 2; int left = getwidth() - frame_width; mviewarea = new rect(left, top, frame_width + left, frame_height + top); } mdraginterwell = (int)(mviewarea.width() / (2 * mtotalframes)); if( mviewupdater == null) { init(); } mviewupdater.start(); minteractionlistener.start(); } /* (non-javadoc) * @see android.view.surfaceholder.callback#surfacedestroyed(android.view.surfaceholder) * @since feb 13, 2013 * @author rajeshcp */ @override public void surfacedestroyed(surfaceholder holder) { if(mviewupdater != null) { mviewupdater.setrun(false); mviewupdater = null; minteractionlistener.misrunning = false; minteractionlistener = null; } } private boolean isscrolled = false; /* (non-javadoc) * @see android.view.view.ontouchlistener#ontouch(android.view.view, android.view.motionevent) * @since feb 13, 2013 * @author rajeshcp */ @override public boolean ontouch(view v, motionevent event) { minteractionlistener.queueevent(event); homecoming true; } /** * @param velocity of type int * @return of type null * function initiate * fling animation * @since feb 15, 2013 * @author rajeshcp */ private void initiatefling(int velocity) { return; // int maxduration = mmaxframeskip * msnapvelocity; // velocity = math.max(-maxduration, math.min(maxduration, velocity)); // int end = ((int)( velocity / msnapvelocity )) * mtotalframes; // // if( mvalueanimator == null ) // { // mvalueanimator = new iqtweener(mcurrentframeindex, mtotalframes - 1); // mvalueanimator.setmlistener(this); // } // mvalueanimator.setmduration(2000); // mvalueanimator.setmstart(mcurrentframeindex); // mvalueanimator.setmend(end); // mvalueanimator.start(); } /** * @param of type null * @return of type null * function clear * touch values * @since feb 15, 2013 * @author rajeshcp */ private void ontouchend() { isscrolled = false; mvelocitytracker.clear(); mvelocitytracker.recycle(); mvelocitytracker = null; minitialtouchx = 0; } /* (non-javadoc) * @see com.inkoniq.iqpromomultitouch.animations.iqtweener.tweenlistener#onupdate(java.lang.object) * @since feb 15, 2013 * @author rajeshcp */ @override public void onupdate(float mcurrent) { final int mindex = (int) mcurrent; if( mindex > (mtotalframes - 1) ) { mcurrentframeindex = (mindex % ( mtotalframes - 1) ); }else if( mindex < 0 ) { mcurrentframeindex = (mtotalframes - 1) + (mindex % (mtotalframes - 1)); } } /** * @param of type null * @return of type null * function load next frame local storage * @since feb 13, 2013 * @author rajeshcp */ private void loadnextframe() { final bitmap frame = getframe(mcurrentframeindex); if(mcurrentframe != null) { bitmap temp = mcurrentframe; mcurrentframe = frame; temp.recycle(); temp = null; }else { mcurrentframe = frame; } } /** * @author rajeshcp * class take care of updating view * @since 13 feb 2013 */ private class viewupdater extends thread { private surfaceholder surface; private boolean run = true; /** * @param run of type boolean * @return of type null * setter function run * @since 13 feb 2013 */ public void setrun(boolean run) { this.run = run; } /** * @param surface of type surfaceholder * @param gameview of type gameview * constructor function * @since 13 feb 2013 */ public viewupdater() { } /* * (non-javadoc) * @see java.lang.thread#run() * @since feb 13, 2013 * @author rajeshcp */ @override public void run() { canvas canvas; while (run) { canvas = null; seek { surface = getholder(); canvas = surface.lockcanvas(null); synchronized (surface) { ondraw(canvas); } } { if (canvas != null) { surface.unlockcanvasandpost(canvas); } } } } } /** * @author rajeshcp * class handle touch * events * @since 19 feb 2013 */ private class interactionlistener extends thread { arraylist<motionevent> meventqueue; boolean misrunning = false; /** * @param of type null * @return of type interactionlistener * constructor function * @since feb 19, 2013 * @author rajeshcp */ public interactionlistener() { init(); } /** * @param of type null * @return of type null * @since feb 19, 2013 * @author rajeshcp */ private void init() { misrunning = true; meventqueue = new arraylist<motionevent>(); } /** * @param event of type motionevent * @return of type null * function add together event * meventqueue * @since feb 19, 2013 * @author rajeshcp */ private void queueevent(motionevent event) { meventqueue.add(event); } /** * @param event of type motionevent * @return of type null * function process touch event * @since feb 19, 2013 * @author rajeshcp */ private void ontouch(motionevent event) { if( event == null ) return; if( mvelocitytracker == null ) { mvelocitytracker = velocitytracker.obtain(); } mvelocitytracker.addmovement(event); if( !mviewarea.contains((int)event.getx(), (int)event.gety()) ) { return; } switch(event.getaction()) { case motionevent.action_down : { if( mvalueanimator != null ) { mvalueanimator.cancel(); } isscrolled = false; minitialtouchx = (int)event.getx(); break; } case motionevent.action_move : { final int currentx = (int)event.getx(); final int draggeddistance = (currentx - minitialtouchx); if( draggeddistance != 0 && draggeddistance % mdraginterwell == 0 ) { isscrolled = true; initiateframechange(draggeddistance / mdraginterwell); log.d(getclass().getname(), "frameindex = " + mcurrentframeindex); minitialtouchx = currentx; } break; } case motionevent.action_up : { if( isscrolled ) { final velocitytracker velocitytracker = mvelocitytracker; velocitytracker.computecurrentvelocity(1000, mmaxvelocity); int velocityx = (int) velocitytracker.getxvelocity(); if( (velocityx > msnapvelocity) || (velocityx < -msnapvelocity) ) { initiatefling(velocityx); } } ontouchend(); break; } case motionevent.action_cancel : { ontouchend(); break; } default : { break; } } } /* (non-javadoc) * @see java.lang.thread#run() * @since feb 19, 2013 * @author rajeshcp */ @override public void run() { while ( misrunning ) { while ( meventqueue.size() > 0 ) { ontouch(meventqueue.remove(0)); seek { thread.sleep(16); }catch (exception e) { } } } } } }
because of complexity of trying display simulated 360 grade view, may want consider using opengl handle rendering. give far more powerfulness on how content displayed , simplify implementation.
however; that's not simple implementation begin with. best way implement this.
android surfaceview
Comments
Post a Comment