TranslateAnimation上ImageView的(安卓) [英] TranslateAnimation on ImageView (Android)

查看:144
本文介绍了TranslateAnimation上ImageView的(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在我的布局ImageView的,我想它滑动到右侧或左侧时,在后者的用户刷卡。我用TranslateAnimation翻译ImageView的,如下图所示。

So I have an ImageView in my layout and I wanted to slide it to the right or left when the user swipe over the latter. I used TranslateAnimation to translate the ImageView, as shown below.

ImageView logoFocus = (ImageView) findViewById(R.id.logoFocus);

Animation animSurprise2Movement = new TranslateAnimation(logoFocus.getLeft(), logoFocus.getLeft()+150, logoFocus.getTop(), logoFocus.getTop());
animSurprise2Movement.setDuration(1000);
animSurprise2Movement.setFillAfter(true);
animSurprise2Movement.setFillEnabled(true);
logoFocus.startAnimation(animSurprise2Movement);

我放在这个code在我向右滑动部分,并在同一code,但使用getLeft() - 150为向左滑动部分。它的工作原理,当我轻扫第一次按预期,但是当我轻扫到另一方向,ImageView的返回到原来的位置,然后滑动在其他方向,而不是仅滑动到原来的位置的

I've placed this code in my Swipe Right section, and the same code but using getLeft()-150 for the Swipe Left section. It works as expected when I swipe a first time, but when I swipe to the other direction, the ImageView goes back to its original position, then slides in the other direction, instead of only sliding to the original position.

我已经尝试添加以下在我设置到动画的AnimationListener的onAnimationEnd方法,但不成功。

I've already tried to add the following in the onAnimationEnd method of an AnimationListener which I've set to the Animation, but in vain.

MarginLayoutParams params = (MarginLayoutParams) logoFocus.getLayoutParams();
params.setMargins(logoFocus.getLeft()+150, logoFocus.getTop(), logoFocus.getRight(), logoFocus.getBottom());
logoFocus.setLayoutParams(params);

我也试过同样的方法以下,但如预期,无论是没有工作。

I've also tried the following in the same method, but that neither didn't work as expected.

((RelativeLayout.LayoutParams) logoFocus.getLayoutParams()).leftMargin += 150;
logoFocus.requestLayout();

会有人帮帮我吗?该位置似乎并没有改变动画后,即使setFillAfter(真)和setFillEnabled(真)使用。是否有替代使用TranslateAnimation?

Would anyone please help me? The position doesn't seem to change after the Animation, even with setFillAfter(true) and setFillEnabled(true) used. Is there an alternative to using TranslateAnimation?

感谢您的帮助,我可以得到的。 :)

Thank you for any help I can get. :)

推荐答案

好了,所以我的工作我的方式。我使用的,而不是试图迫使ImageView的改变其实际位置的全局变量,现在我每次更新我动画ImageView的时间。由于我使用setFillAfter(真)和setFillEnabled(真),不回去无意中到原来的位置了。

Okay, so I worked my way around it. I'm using a global variable now which I update each time I animate the ImageView, instead of trying to force the ImageView to change its actual position. Since I'm using setFillAfter(true) and setFillEnabled(true), it doesn't go back unintentionally to its original position anymore.

private float xCurrentPos, yCurrentPos;
private ImageView logoFocus;

logoFocus = (ImageView) findViewById(R.id.logoFocus); 
xCurrentPos = logoFocus.getLeft(); 
yCurrentPos = logoFocus.getTop(); 

Animation anim= new TranslateAnimation(xCurrentPos, xCurrentPos+150, yCurrentPos, yCurrentPos); 
anim.setDuration(1000); 
anim.setFillAfter(true); 
anim.setFillEnabled(true); 
animSurprise2Movement.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationStart(Animation arg0) {}

    @Override
    public void onAnimationRepeat(Animation arg0) {}

    @Override
    public void onAnimationEnd(Animation arg0) {
        xCurrentPos -= 150;
    }
});
logoFocus.startAnimation(anim);

希望这有助于,如果你有同样的问题。我见过几个职位是这样,没有很好的答案。

Hope this helps if you're having the same problem. I've seen several posts like this with no good answer.

这篇关于TranslateAnimation上ImageView的(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆