如何在Android Studio中缓慢更改视图的x、y位置。

huangapple go评论70阅读模式
英文:

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

问题

以下是翻译好的部分:

我有一个ImageView。当屏幕被触摸时,我的ImageView应该向屏幕上移动。实际上,这是一次跳跃。当我触摸屏幕时,我的角色会跳跃。但它发生得很快。它立即移到了y位置-100。我想让它慢慢移动,就像慢慢跳跃一样,我该怎么做?

anakarakter = 我的ImageView

该方法(Anakarakter)在定时器和处理程序中工作。

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;
        }
    });
}
英文:

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() &lt; gameScreen.getWidth() / 2)
                    {
                        anakarakter.startAnimation(left);
                    }
                    if(event.getX() &gt; gameScreen.getWidth() / 2)
                    {
                        anakarakter.startAnimation(right);
                    }
                    anakarakter.setX(event.getX() - anakarakter.getWidth() / 2);
                    anakarakter.setY(anakarakter.getY() - anakarakter.getHeight() - 50);
                }
                if(anakarakter.getX() &lt; 0 )
                {
                    anakarakter.setX(0);
                }
                if(anakarakter.getX() &gt; gameScreen.getWidth() - anakarakter.getWidth())
                {
                    anakarakter.setX(gameScreen.getWidth() - anakarakter.getWidth());
                }

                return true;
            }
        });
     }

答案1

得分: 0

使用ObjectAnimator来实现这个效果:

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

use an ObjectAnimator for this:

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

huangapple
  • 本文由 发表于 2020年8月10日 09:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63332826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定