Code for moving or dragging an image in sketchware android project
1 minute read
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.