0

I created a method for changing the position of an ImageView, based on the position of other Image.

    public void move(double zx, double zy) {

    RelativeLayout.LayoutParams params =
            new RelativeLayout.LayoutParams(80,80);

    // the imageView s was initialized in the onCreate()


    System.out.println("working");


    if (zx < s.getLeft()) {
        if (s.getLeft() - zx > 0) {
            params.leftMargin = s.getLeft() -1;
        } else
            params.leftMargin = s.getLeft() +1;
    }
    if (zx > s.getLeft()) {
        if (s.getLeft() - zx > 0) {
            params.leftMargin = s.getLeft() -1;
        } else
            params.leftMargin = s.getLeft() +1;
    }
    if (zy > s.getTop()) {
        if (s.getTop() - zy > 0) {
            params.topMargin = s.getTop() -1;
        } else
            params.topMargin = s.getTop() + 1;
    }
    if (zy < (int)s.getY()) {
        if ((int)s.getY() - zy > 0) {
            params.topMargin = s.getTop() -1;
        } else
            params.topMargin = s.getTop() + 1;
    }

    s.setLayoutParams(params);
}

Then I called in the onCreate()

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fase);

    s = (ImageView)findViewById(R.id.s);
    box = (ImageView)findViewById(R.id.box);

    move(box.getLeft(),box.getTop());

    }

The message "working" was printed, but only once. The imageview's position also didn't change. My conclusion is that the method wasn't executed repeatedly. Otherwise, the imageView's position would be changing every time, and the message "working" would appear each instant. How can I solve it? Should I call the method in other class? I tried to call it in a timertask and execute the timertask each milisecond, but tha application just stopped.

Drag command for the other ImageView (inside oncreate):

        box.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            LayoutParams layoutParams = (LayoutParams) box.getLayoutParams();

            switch(event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    System.out.println(box.getLeft());
                    int x_cord = (int)event.getRawX();
                    int y_cord = (int)event.getRawY();

                    if(x_cord>windowwidth){x_cord=windowwidth;}
                    if(y_cord>windowheight){y_cord=windowheight;}

                    layoutParams.leftMargin = x_cord - 250; 
                    layoutParams.topMargin = y_cord - 300; 

                    box.setLayoutParams(layoutParams);
                    break;
                default:
                    break;
            }
            return true;
        }
    });
Danilo
  • 11
  • 1
  • 5

1 Answers1

0

First, you never told your program to execute your method move more than once when onCreate is called. Second timertask each milisecond.. i think this will be way to much executing move 1000 times each second .. and third using timertask can lead to many Problems, you can use handler instead for example.

Have a look at this, it shows you how to implement a handler for repeating Tasks!

Community
  • 1
  • 1
Mike
  • 857
  • 1
  • 7
  • 11
  • I worked with the handler, but the result was unexpected. The imageView moved, but only while I was dragging the other imageView! Just for a test, I wrote "params.topMargin ++" in the runnable, and initially the imageView didn't move, but when I started to drag the other ImageView, it started to work! And When I stopped dragging, the other imageView also stopped moving. I will add the drag command to the question. – Danilo Jan 06 '15 at 22:04