android - Moving the image according to the Button click -
i used 4 buttons (up,down,left,right) , have image. have move image accordingly when press button should move image in upward direction , when press left remaining both direction should move image in left direction.i have used onclick listener , trying move image using x,y co-ordinates.i don't how take x,y co-ordinates.
here code.
public class mainactivity extends activity implements onclicklistener { button up,left,right,down; imageview i1; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); up=(button)findviewbyid(r.id.button1); left=(button)findviewbyid(r.id.button2); right=(button)findviewbyid(r.id.button3); down=(button)findviewbyid(r.id.button4); i1=(imageview)findviewbyid(r.id.imageview1); up.setonclicklistener(this); } public void onclick(view arg0) { toast.maketext(getapplication(),"up",5000).show(); relativelayout.layoutparams mparams = (relativelayout.layoutparams) i1.getlayoutparams(); int x = (int)getrawx(); int y = (int)getrawy(); mparams.leftmargin = x-50; mparams.topmargin = y-50; i1.setlayoutparams(mparams); } } hi, have updated code below please check it.
package com.example.motion;
public class mainactivity extends activity implements onclicklistener { button up,left,right,down; imageview i1; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); up=(button)findviewbyid(r.id.button1); left=(button)findviewbyid(r.id.button2); right=(button)findviewbyid(r.id.button3); down=(button)findviewbyid(r.id.button4); i1=(imageview)findviewbyid(r.id.imageview1); up.setonclicklistener(this); down.setonclicklistener(this); left.setonclicklistener(this); right.setonclicklistener(this); } public void onclick(view v) { // todo auto-generated method stub switch(v.getid()) { case r.id.button1: { toast.maketext(getapplication(),"up",toast.length_short).show(); relativelayout.layoutparams mparams = (relativelayout.layoutparams) i1.getlayoutparams(); mparams.topmargin -= 20; i1.setlayoutparams(mparams); break; } case r.id.button4: { toast.maketext(getapplication(),"down",toast.length_short).show(); relativelayout.layoutparams mparams = (relativelayout.layoutparams) i1.getlayoutparams(); mparams.topmargin += 20; i1.setlayoutparams(mparams); break; } case r.id.button2: { toast.maketext(getapplication(),"left",toast.length_short).show(); relativelayout.layoutparams mparams = (relativelayout.layoutparams) i1.getlayoutparams(); mparams.leftmargin -= 20; i1.setlayoutparams(mparams); break; } case r.id.button3: { toast.maketext(getapplication(),"right",toast.length_short).show(); relativelayout.layoutparams mparams = (relativelayout.layoutparams) i1.getlayoutparams(); mparams.leftmargin += 20; i1.setlayoutparams(mparams); break; } } } }
i think problem (int)getrawx(); , (int)getrawy();, calling methods on activity , i'm not sure need values.
you try:
mparams.leftmargin += 50; or
int x = mparams.leftmargin; mparams.leftmargin = x + 50
Comments
Post a Comment