34 Animation and transitioning

0 Yaron X · April 12, 2015
I tried to modify the code from Bucky's tutorial #34 , so when the I touch the screen the button moves from top-left to right-bottom. And if I touch the screen again then the button will move from right-bottom to top-left.
But what is happening is that when I touch the screen the button starts moving from TL to BR but then it jumps right back to the TL corner! 
The longer touch the screen the longer button "travels" towards the BL corner. 
Why is that? And what must I change in order for the button to move in one direction the 1st time I touch the screen, and in the other direction the 2nd time I touch?



    public   void moveButton() {
        View theButton = findViewById(R.id.button1);

        TransitionManager.beginDelayedTransition(myLayout); //for smooth transitioning

        //Change position
        RelativeLayout.LayoutParams posRules = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        if ( state)         {
            posRules.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            posRules.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        }
        else {
            posRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            posRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }

        theButton.setLayoutParams(posRules);

        //Change size
        ViewGroup.LayoutParams sizeRules = theButton.getLayoutParams();
        if ( state) {
            sizeRules.width = 250;
            sizeRules.height = 100;

        }
        else {
            sizeRules.width = 450;
            sizeRules.height = 300;
        }
        theButton.setLayoutParams(sizeRules);

        state = !state;
    }


Post a Reply

Replies

Oldest  Newest  Rating
0 Yaron X · April 17, 2015
Solved by  checking if the action is ACTION_DOWN


if (event.getAction() == MotionEvent.ACTION_DOWN) {
           moveButton();
       }
  • 1

Java / Android Development

107,006 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator