Android: Rotate two objects(circles) onTouch with animation -
basically want draw 2 circles (one red, 1 blue) , make them rotate rotate animation. trying days without success.
tried move animation method gameview class didn't work there either.
currently crashes on ontouch. here's code far:
gamescreen
public class gamescreen extends activity { private gameview mygameview; private drawthread drawthread ; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_game_screen); mygameview = (gameview) (findviewbyid(r.id.gameview1)); mygameview.setzorderontop(true); mygameview.getholder().setformat(pixelformat.translucent); } public boolean ontouchevent(motionevent e) { int eventaction = e.getaction(); switch (eventaction) { case motionevent.action_down: system.out.println("clicked"); mygameview.rotate(mygameview); mygameview.invalidate(); break; } return true; } }
gameview
public class gameview extends surfaceview implements surfaceholder.callback { private bleencatcher bluecatcher; private bleencatcher redcatcher; private drawthread drawthread; private int colorblue = -16776961; private int colorred = -65536; public gameview(context context) { super(context); initialize(); } public gameview(context context, attributeset attrs) { super(context, attrs); initialize(); } public gameview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); initialize(); } private void initialize() { getholder().addcallback(this); drawthread = new drawthread(getholder(), this); setfocusable(true); } public void surfacecreated(surfaceholder holder) { bluecatcher = new bleencatcher(540, 850, colorblue); redcatcher = new bleencatcher(540, 950, colorred); drawthread.setrunning(true); drawthread.start(); } public void surfacechanged(surfaceholder arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } public void surfacedestroyed(surfaceholder arg0) { boolean retry = true; drawthread.setrunning(false); while (retry) { try { drawthread.join(); retry = false; } catch (interruptedexception e) { } } } public void ondraw(canvas canvas) { redcatcher.ondraw(canvas); bluecatcher.ondraw(canvas); } public void rotate(gameview game ){ redcatcher.changeposition(game); bluecatcher.changeposition(game); } }
bleencatcher
public class bleencatcher { int x, y, rad = 50; private animation anima; private boolean rotate = false; private gameview mygameview; bleenpaint catcherpaint; public bleencatcher(int x, int y, int color) { this.x = x; this.y = y; catcherpaint = new bleenpaint(1, paint.cap.square, paint.style.fill, color); } public void ondraw(canvas canvas) { if(rotate){ canvas.drawcircle(x, y, rad, catcherpaint); createanimation(canvas); rotate = false; }else{ canvas.drawcircle(x, y, rad, catcherpaint); } } public void changeposition(gameview game){ rotate = true; mygameview = game; } protected void createanimation(canvas canvas) { anima = new rotateanimation(0, 180, 540 , 850); anima.setrepeatmode(animation.restart); anima.setrepeatcount(animation.infinite); anima.setduration(1000l); mygameview.startanimation(anima); } }
drawthread
public class drawthread extends thread { private surfaceholder surfaceholder; private gameview surfaceview; private boolean run = false; public drawthread(surfaceholder surfaceholder, gameview surfaceview) { this.surfaceholder = surfaceholder; this.surfaceview = surfaceview; run = false; } public void setrunning(boolean run) { log.d("setrunning@drawthread", "run status " + run); this.run = run; } @override public void run() { canvas canvas = null; while (run) { try { canvas = surfaceholder.lockcanvas(null); synchronized (surfaceholder) { surfaceview.ondraw(canvas); } } { if (canvas != null) { surfaceholder.unlockcanvasandpost(canvas); } } } } }
bleenpaint
public class bleenpaint extends paint { public bleenpaint(int strokewidth, paint.cap cap, paint.style style, int color) { setstrokewidth(strokewidth); setantialias(true); setstrokecap(cap); setstyle(style); setcolor(color); } }
any appreciated.
make anim folder in res , make flipping.xml , use in class.
<set android:interpolator="@android:anim/linear_interpolator"> <rotate android:fromdegrees="0" android:todegrees="360" android:pivotx="50%" android:pivoty="50%" android:duration="500" android:startoffset="0" android:repeatcount="1" android:repeatmode="reverse"/></set>
like in code & manage time how time want animation.
animatorset set; set = (animatorset) animatorinflater.loadanimator(this, r.anim.flipping); set.settarget(img_logo); set.start(); timer timer = new timer(); timer.schedule(task, 2000);
Comments
Post a Comment