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.


Comments

  1. Wut type of code is this up....
    The code we need to put in add source directly

    ReplyDelete
  2. Wut type of code is this up....
    The code we need to put in add source directly

    ReplyDelete
    Replies
    1. Yes. Just rename Imageview from Imageview1 to img.
      Then put the code above in add source directly block.

      Delete
    2. ¡¡¡You are a genius!!! thank you very much. We expect more things to do with this block...Awesome!!!

      Delete
  3. But how do I drag the image over top of another picture? I just got the app

    ReplyDelete
  4. There's no add block anymore how can I make one

    ReplyDelete
  5. Hi all my genius android developers.
    I 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.

    ReplyDelete
    Replies
    1. Make a list of urls of your musics and then you can play them.

      Delete
  6. Works perfect. But how to stop "img" going over the border of liniar.

    ReplyDelete
  7. Works perfect. But how to stop "img" going over the border of liniar.

    ReplyDelete
  8. how do you get the dragged image back to the original position if it's not dropped at a certain position?
    I 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

    ReplyDelete
  9. Si i have 3 lineair layouts.
    And when i move the image to the second lineair IT disapears.
    Can you please help me.

    ReplyDelete
  10. It working but if i could get the code step by step. I really don't understand coding like those.

    ReplyDelete
  11. i want to make a game. how can i move the hero by clicking bottom ? please help brother !

    ReplyDelete
  12. can we use it as auto click?

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Enable Fullscreen for Youtube videos in WebView

Creating a Drawing View in Sketchware

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware