android - How to move image on screen? -
i want move image on screen , able that, not properly. image goes downward fine , want start going upward in direction once has moved bottom of screen.
here have tried. in code below, margenmaxx
width of screen , margenmaxy
height of screen
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // setcontentview(r.layout.activity_main); thread mythread = new thread(new updatethread()); mythread.start(); public class updatethread implements runnable { @override public void run() { //... code manipulate position while (i<margenmaxx){ if(j<margenmaxy) { try { runonuithread(new runnable() { @override public void run() { /*mdrawable.setbounds(i, j ,i+ width, i+ height); mdrawable.draw(cc); invalidate();*/ } }); thread.sleep(200); i=i+10; j=j+10; } catch (interruptedexception e) { e.printstacktrace(); } }else if(j>=margenmaxy-height){ try { runonuithread(new runnable() { @override public void run() { /*mdrawable.setbounds(i, j ,i+ width, i+ height); mdrawable.draw(cc); invalidate();*/ } }); thread.sleep(200); i=i-10; j=j-10; } catch (interruptedexception e) { e.printstacktrace(); } } } } } public class animatedview extends imageview { public animatedview(context context) { super(context); // todo auto-generated constructor stub mdrawable = new shapedrawable(new ovalshape()); mdrawable.getpaint().setcolor(0xffffac23); } protected void ondraw(final canvas cc) { final context context = null; mdrawable.setbounds(i, j ,i+ width, i+ height); mdrawable.draw(cc); invalidate(); } }
update 1:
using code, ball going upwards , side after hits ground. now, want ball come when hits right boundary. did coding not coming back. final goal develop game in ball must come either left or right. must hit ground , go in opposite direction, hit wall , come back. ball must work long game going on.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // setcontentview(r.layout.activity_main); thread mythread = new thread(new updatethread()); mythread.start(); public class updatethread implements runnable { boolean mmovedown=false; boolean mmoveup = false; @override public void run() { while(!mmoveup) { // move image down , right. try { runonuithread(new runnable() { @override public void run() { } }); thread.sleep(200); i=i+10; j=j+10; // try posting runnable ui thread update view. } catch (interruptedexception e) { e.printstacktrace(); }if(j >= margenmaxy) { // change moving phase. mmoveup = true; } } while(mmoveup){ try { runonuithread(new runnable() { @override public void run() { } }); thread.sleep(200); i=i + 10; j=j - 10; } catch (interruptedexception e) { e.printstacktrace(); } if(i >= margenmaxx) { // change moving phase. mmovedown = true; } }while(mmovedown){ try { runonuithread(new runnable() { @override public void run() { } }); thread.sleep(200); i=i - 10; j=j + 10; } catch (interruptedexception e) { e.printstacktrace(); } } } } public class animatedview extends imageview { public animatedview(context context) { super(context); // todo auto-generated constructor stub mdrawable = new shapedrawable(new ovalshape()); mdrawable.getpaint().setcolor(0xffffac23); } protected void ondraw(final canvas cc) { final context context = null; mdrawable.setbounds(i, j ,i+ width, j+ height); mdrawable.draw(cc); invalidate(); } }
this code has 2 conditional statements overlap. however, second 1 checked if first fails. second condition is:
(j >= margenmaxy - height)
this automatically implies (j < margenmaxy) because margenmaxy, , height both positive. when check condition this:
if(j<margenmaxy) { // downward animation. } else if(j>=margenmaxy-height){ // upward animation. }
what happens expect happen phase image moves down. try move image up, (j < margenmaxy) condition satisfied again, , code tries move image down. moment move down, first condition no longer valid. since have construct inside loop, results in "bouncing" behaviour.
to sort problem out, need change logic. 1 simple way have boolean hold state of animation. state variable changed depending on whether image has hit screen's bottom or not.
public class updatethread implements runnable { // variable store animation state. boolean mmoveup = false; @override public void run() { //... code manipulate position while (i<margenmaxx) { // if animation in moving down phase if(!mmoveup) { // move image down , right. += 10; j += 10; // try posting runnable ui thread update view. try { runonuithread(new runnable() { @override public void run() { /*mdrawable.setbounds(i, j ,i+ width, i+ height); mdrawable.draw(cc); invalidate();*/ } }); thread.sleep(200); } catch (interruptedexception e) { e.printstacktrace(); } } // we're moving upwards. else { // move , left -= 10; j -= 10; try { runonuithread(new runnable() { @override public void run() { /*mdrawable.setbounds(i, j ,i+ width, i+ height); mdrawable.draw(cc); invalidate();*/ } }); thread.sleep(200); } catch (interruptedexception e) { e.printstacktrace(); } // catch! } // else! // check see if we've hit bottom of screen if(j >= margenmaxy - height) { // change moving phase. mmoveup = true; } // note, can use altered form of switch , forth // between moving , down. } // while(i < margenmaxx)! } // run()! } // updatethread!
as added note, since moving ball left , right, might end hitting boundaries well. should implement better checking that. second note, not best way accomplish moving image in android. , finally, more general solution use android property animation interpolator want. if want once, view animation may suffice.
Comments
Post a Comment