Code for moving or dragging an image in sketchware android project
Now with the introduction of add source directly block in Sketchware, several such actions are possible which were not possible earlier. One such action is dragging an image by touching it.
It is very simple to move an image with movement of finger.
First insert an ImageView in VIEW area, inside any Layout. Name it as img. Upload image using image manager and set the image in ImageView.
After that in LOGIC area in onCreate event, use operator block add source directly, and add the following code:
img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override public boolean onTouch(View v, MotionEvent event) { int eid = event.getAction(); switch (eid) {
case MotionEvent.ACTION_MOVE : PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
img.setX((int)(StartPT.x+mv.x)); img.setY((int)(StartPT.y+mv.y));
StartPT = new PointF( img.getX(), img.getY() ); break;
case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF( img.getX(), img.getY() ); break;
case MotionEvent.ACTION_UP : break; default : break;} return true;} });
Now save and run the project. You will be able to move the image with your finger.
It is very simple to move an image with movement of finger.
First insert an ImageView in VIEW area, inside any Layout. Name it as img. Upload image using image manager and set the image in ImageView.
After that in LOGIC area in onCreate event, use operator block add source directly, and add the following code:
img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override public boolean onTouch(View v, MotionEvent event) { int eid = event.getAction(); switch (eid) {
case MotionEvent.ACTION_MOVE : PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
img.setX((int)(StartPT.x+mv.x)); img.setY((int)(StartPT.y+mv.y));
StartPT = new PointF( img.getX(), img.getY() ); break;
case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF( img.getX(), img.getY() ); break;
case MotionEvent.ACTION_UP : break; default : break;} return true;} });
Now save and run the project. You will be able to move the image with your finger.
Great !! Work perfectly 👍
ReplyDeleteWut type of code is this up....
ReplyDeleteThe code we need to put in add source directly
Wut type of code is this up....
ReplyDeleteThe code we need to put in add source directly
Yes. Just rename Imageview from Imageview1 to img.
DeleteThen put the code above in add source directly block.
¡¡¡You are a genius!!! thank you very much. We expect more things to do with this block...Awesome!!!
DeleteThank
Deletehii bro i need ur help
ReplyDeleteThanks 👍👍👌
ReplyDeleteBut how do I drag the image over top of another picture? I just got the app
ReplyDeleteKeep both pictures in same LinearLayout.
DeleteThere's no add block anymore how can I make one
ReplyDeleteHi all my genius android developers.
ReplyDeleteI need some help ,my problem is i am building a music streaming app i had completed my media player layout and all performing actions by it like play pause stop frwrd bkwrd and also seekbaar .
Currently i know only method to stream music via url but main problem is it only play that single music from url but i want to play all music from my website.
Make a list of urls of your musics and then you can play them.
DeleteWorks perfect. But how to stop "img" going over the border of liniar.
ReplyDeleteWorks perfect. But how to stop "img" going over the border of liniar.
ReplyDeleteObrigado!! Perfeito!
ReplyDeletehow do you get the dragged image back to the original position if it's not dropped at a certain position?
ReplyDeleteI mean like a puzzle game, you will understand.
I want to imply the drag and drop function like that, so when the img that is dropped does not match the given position, then the img returns to its original position, and if the dragged img is dropped at the correct position then img had moved
Do you have answer for this??
DeleteSi i have 3 lineair layouts.
ReplyDeleteAnd when i move the image to the second lineair IT disapears.
Can you please help me.
It working but if i could get the code step by step. I really don't understand coding like those.
ReplyDeletei want to make a game. how can i move the hero by clicking bottom ? please help brother !
ReplyDeletecan we use it as auto click?
ReplyDelete