2

EDIT: Cliffnote of what i want accomplished straight to the point, My XML set up is has 2 linearview (top, bottom) 1. I want my img to stay within screen when moving 2. Moving continuously randomly 3. Have it clickable/draggable 4. Have the img get its new position and move whereever it is dropped (top or bottom) Thank you.

Hello making an app (Android Studio) for autistic children (inspired by my autistic son). In my Activity i have 2 linear layout: Top and Bottom. The top is where 3/4 of my screen that has 4 images. The bottom is the targeted drop point where the "Answer" or img should be dropped.

I have an 4 img randomly created in a (via databaseFP iterated) but now i want those img to be moving randomly within screen (top screen) and drag them into a target drag box (bottom screen). Only 1 of four img is the correct "answer" so if its "incorrect" i want this img to return to its position MOVING all the while within screen until the "correct" img is chosen to the targeted drop box. That is all...Please Please help me i have read many and not like this specifically where it involves onTouch/onDrag listener and the img goes back to position if dragged.

So far all i have done is make the img move (it retains the on click and drag but when its incorrect it goes off screen) Pls see code below

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;

Random r = new Random();

int distance = 100; //the distance to move in pixels
int duration = 500; //the duration of the animation in ms

double direction = Math.random() * 2 * Math.PI;

int translationX = (int) (Math.cos(direction) * distance);
int translationY = (int) (Math.sin(direction) * distance);
int id2 = getResources().getIdentifier(myShuffledArray[0], "drawable", getPackageName());
image.setImageResource(id2);
image.animate().translationX(translationX - width).translationY(translationY - height).setDuration(duration).start();

I am a newbie but this would help my autistic son and this is a Thesis project for my college.

EDIT this is my ontouch/drag

 public boolean onDrag(View v, DragEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction()==DragEvent.ACTION_DROP){
        //we want to make sure it is dropped only to left and right parent view
        View view = (View)event.getLocalState();
        //if(v.getId() == R.id.left_view || v.getId() == R.id.right_view){
            if(view.getId() == R.id.box_view1){
                ViewGroup source = (ViewGroup) view.getParent();
                source.removeView(view);
                LinearLayout target = (LinearLayout) v;
                target.addView(view);
            }
            //make view visible as we set visibility to invisible while starting drag
            view.setVisibility(View.VISIBLE);
        }
        return true;
    }

@Override
public boolean onTouch(View view, MotionEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    }
    return false;
}
Mark Ligot
  • 67
  • 7
  • so what part are you having trouble with? your question is a bit long winded – petey Jan 05 '17 at 15:54
  • I just cant get the img to move within screen CONTINUOUSLY....PLUS when it is touched and the img was incorrect i want it to retain its position to keep moving. at the moment the img disappears completely... This code i got from other questions just implemented to my project. – Mark Ligot Jan 05 '17 at 15:58

1 Answers1

0

I have found the solution posted on this website https://blahti.wordpress.com/2011/01/17/moving-views-part-2/ Check it out for those who wants learn more on Views and Drag and Drop

Mark Ligot
  • 67
  • 7