英文:
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() < 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;
}
});
}
答案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, "translationY", 0f, 100f,0f);
animY.setDuration(1000);//1sec
animY.setInterpolator(new BounceInterpolator());
animY.setRepeatCount(0);
animY.start();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论