简体   繁体   中英

how to change a view's x, y positions slowly in android studio

there is an ImageView i have. when screen gets touch then my imageview should move upward on screen. Actually its a jump. when i touch to screen my character jumps. but it happens instantly. it instantly going to -100 in y positions. i want to make it slowly, like slowly jump how can i do that?

anakarakter = my ImageView

that method(Anakarakter) works in a timer, handler.

    public void Anakarakter ()
    {
        gameScreen.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    if(event.getX() < gameScreen.getWidth() / 2)
                    {
                        anakarakter.startAnimation(left);
                    }
                    if(event.getX() > gameScreen.getWidth() / 2)
                    {
                        anakarakter.startAnimation(right);
                    }
                    anakarakter.setX(event.getX() - anakarakter.getWidth() / 2);
                    anakarakter.setY(anakarakter.getY() - anakarakter.getHeight() - 50);
                }
                if(anakarakter.getX() < 0 )
                {
                    anakarakter.setX(0);
                }
                if(anakarakter.getX() > gameScreen.getWidth() - anakarakter.getWidth())
                {
                    anakarakter.setX(gameScreen.getWidth() - anakarakter.getWidth());
                }

                return true;
            }
        });
     }

use an ObjectAnimator for this:

ObjectAnimator animY = ObjectAnimator.ofFloat(your_imageview, "translationY", 0f, 100f,0f);
                animY.setDuration(1000);//1sec
                animY.setInterpolator(new BounceInterpolator());
                animY.setRepeatCount(0);
                animY.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM