Android, trying to figure out how animate a bitmap, have timer working, but don't know how to update screen -
i got sample code update text control of timer. next created custom view , override ondraw have animation view. custom view has bitmap move down 1 pixel every time timer goes off more fancy after working.
i thinking calling custom views invalidate timer call, ondraw function called. not know if coming timer, meaning have move bitmap, or android updating screen.
what best way this??? code timer
package besttecksolutions.tellafortune; ///////////// import java.util.timer; import java.util.timertask; import android.app.activity; import android.os.bundle; import android.os.handler; import android.os.message; import android.os.handler.callback; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.linearlayout; import android.widget.textview; public class cshuffle2 extends activity { textview text3; long starttime = 0; //this posts message main thread our timertask //and updates textfield //runs without timer reposting self handler h2 = new handler(); runnable run = new runnable() { @override public void run() { long millis = system.currenttimemillis() - starttime; int seconds = (int) (millis / 1000); int minutes = seconds / 60; seconds = seconds % 60; text3.settext(string.format("%d:%02d", minutes, seconds)); h2.postdelayed(this, 500); } }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.shuffle2); text3 = (textview)findviewbyid(r.id.text3); h2.postdelayed(run, 0); linearlayout layout = (linearlayout)findviewbyid(r.id.mainlayout); // add adview cshuffleview2 myview; myview=new cshuffleview2(this); layout.addview(myview); } } custom view class animation
package besttecksolutions.tellafortune; import android.content.res.assetmanager; import android.graphics.bitmap; import android.graphics.bitmapfactory; import java.io.inputstream; import java.io.ioexception; import android.app.activity; import android.content.context; import android.graphics.canvas; import android.graphics.paint; import android.graphics.rect; import android.content.intent; import android.content.context; import android.os.bundle; import android.view.view; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.os.bundle; import android.view.*; import android.view.view.onclicklistener; public class cshuffleview2 extends view { view t; bitmap icon; cshuffleview2( context context) { super(context); // load in bitmaps try { assetmanager assetmanager= context.getassets(); inputstream inputstream; inputstream=assetmanager.open("bio.png"); icon=bitmapfactory.decodestream(inputstream); inputstream.close(); } catch( ioexception e) { } } protected void ondraw( canvas canvas) { paint color = new paint(); color.setcolor(0xffffff00); canvas.drawrect(0,0, getwidth(),getheight(),color); canvas.drawbitmap(icon, 100,100,null); } }
create public boolean field, or private get/set, , set true in timer callback (e.g. myview.updatebitmap = true;). in ondraw, can test it:
if (this.updatebitmap){ // update bitmap this.updatebitmap = false; } updatebitmap can true if set in timer callback (assuming don't use anywhere else).
even better implement own callback on extended view call directly timer.
Comments
Post a Comment