jpm*_*ind 3 android position imageview android-animation translate-animation
所以我的布局中有一个ImageView,当用户滑过后者时我想向右或向左滑动它.我使用TranslateAnimation来翻译ImageView,如下所示.
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);
Run Code Online (Sandbox Code Playgroud)
我已将此代码放在我的"向右滑动"部分,并使用相同的代码但使用getLeft() - 150用于"向左滑动"部分.当我第一次滑动时,它按预期工作,但当我向另一个方向滑动时,ImageView返回到其原始位置,然后向另一个方向滑动,而不是仅滑动到原始位置.
我已经尝试在AnimationListener的onAnimationEnd方法中添加以下内容,我将其设置为动画,但是徒劳无功.
MarginLayoutParams params = (MarginLayoutParams) logoFocus.getLayoutParams();
params.setMargins(logoFocus.getLeft()+150, logoFocus.getTop(), logoFocus.getRight(), logoFocus.getBottom());
logoFocus.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
我也尝试过以下相同的方法,但是这两种方法都没有按预期工作.
((RelativeLayout.LayoutParams) logoFocus.getLayoutParams()).leftMargin += 150;
logoFocus.requestLayout();
Run Code Online (Sandbox Code Playgroud)
有人请帮帮我吗?即使使用了setFillAfter(true)和setFillEnabled(true),动画后的位置似乎也没有变化.有没有使用TranslateAnimation的替代方案?
谢谢你的帮助.:)
jpm*_*ind 13
好的,所以我一直在努力.我正在使用一个全局变量,每当我为ImageView设置动画时我都会更新,而不是试图强制ImageView更改其实际位置.由于我使用的是setFillAfter(true)和setFillEnabled(true),因此它不再无意中回到原来的位置.
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);
Run Code Online (Sandbox Code Playgroud)
希望如果您遇到同样的问题,这会有所帮助.我看过几个这样的帖子没有好的答案.
| 归档时间: |
|
| 查看次数: |
9462 次 |
| 最近记录: |