1

I know how to use dragEvent for dragging an imageView's shadow by the screen. But what I want to do is to move the imageView, changing it's coordinates. When I drag an image, it's left and top don't change (I created a timertask for printing the value regularly, and even when I'm dragging the image the coordinates don't change). It happens because I am moving the shadow, but I'd like to use a drag event for moving the imageView itself.

Methods:

private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    } else {
        return false;
    }
}

}

class MyDragListener implements View.OnDragListener {
Drawable enterShape = getResources().getDrawable(R.drawable.box);
Drawable normalShape = getResources().getDrawable(R.drawable.box);

@Override
public boolean onDrag(View v, DragEvent event) {
    int action = event.getAction();
    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            System.out.println("Action is DragEvent.ACTION_DRAG_STARTED");
            Log.v(LOGS, "box " + box.getX());
            // do nothing
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            System.out.println("Action is DragEvent.ACTION_DRAG_ENTERED");
            Log.v(LOGS, "box " + box.getX());
            //v.setBackgroundDrawable(enterShape);
            v.setBackgroundResource(R.drawable.box);
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            System.out.println("Action is DragEvent.ACTION_DRAG_FINISHED");
            Log.v(LOGS, "box " + box.getX());
            //v.setBackgroundDrawable(normalShape);
            v.setBackgroundResource(R.drawable.box);
            break;
        case DragEvent.ACTION_DROP:
            // Dropped, reassign View to ViewGroup
            View view = (View) event.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            //v.setBackgroundDrawable(normalShape);
        default:
            break;
    }
    return true;
}

}

Danilo
  • 11
  • 1
  • 5
  • see my answer here: http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue – pskink Dec 23 '14 at 23:10
  • I have an idea: I may create a gesture detector to check if any point near the imageView was pressed, and command the image to move to the pressed point. If a distant point was pressed, nothing would happen. Then, I'd need a bitmap, an onTouchListener and probably something else. It would be just like a drag event. I just want to adapt a project I created in java (which works perfectly in my computer) to android, and I started to search for a way to do it a few weeks ago. I'm really "noob" on it and this solution may be wrong, then tell em if it really may work. – Danilo Dec 24 '14 at 00:15
  • what do you need that ImageView for? – pskink Dec 24 '14 at 00:17
  • My project is a game. This ImageView is the controlable object. The player will drag it over the screen and try to avoid colision with other objects (different ImageViews) that have their own movement, based on the controlable ImageView's x and y. – Danilo Dec 24 '14 at 00:46
  • drag the Bitmap not ImageView, why to make it slower? – pskink Dec 24 '14 at 00:50
  • Well, the not controlable ImageView moves based on the controlable ImageView's coordinates. The move method uses the ControlableImageView.getLeft() and ControlableImageView.getTop() as parameters to follow. The problem is that when I drag the image the getLeft() and getTop() doesn't change, but if I can fix it by moving the bitmap and returning it's coordinates, I'll try to do it. – Danilo Dec 24 '14 at 01:43

0 Answers0