我明白,这是本地问题,但我已经与它斗争了 1 天,我想请求你的帮助。
我有这样一种“游戏”,用户可以通过 swype 移动元素,在这种情况下,我们正在从上到下进行 swype。项目是实际的ImageView。
在第一张图片上,我们可以在实际 swype 之前看到 7 个项目
项目尽可能向下移动,直到遇到另一个项目或 field 边界(现在不可见)
在第二张图片中,元素已向下移动一次(仅那些没有移动障碍物的元素,其中 4 个)
并且,第二次滑动后项目的最终位置:
现在的问题是:我无法解释为什么,但是当我第一次 swype时,从上到下,并应用到这4个项目,它们会将过渡的实际动画向下移动到新的位置,他们半路消失,出现在新的地方!
当我向任何方向进行任何其他挥动时,任何项目都会动画化,没有问题。当我第二次滑动到底部时,一切都很顺利。
我检查了一遍又一遍,动画正确完成,参数正确,动画途中没有其他 ImageView,但是有东西正好覆盖了动画,以防 ImageView 顶部有障碍物!这很奇怪。
所有需要的代码如下:
动画无效
private void moveItem(final FieldItem item, final float x, final float y) {
// item to animate, transition X, transition Y
final RelativeLayout.LayoutParams oldParams = (LayoutParams) item
.getLayoutParams();
TranslateAnimation animation = new TranslateAnimation(0.0f, x, 0.0f, y);
animation.setDuration(500);
animation.setFillAfter(true);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
item.invalidate();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
item.clearAnimation();
//all following is just to remove old item from layout
//and put new item on the place where animation should have ended
//nothing which can affect, it works perfect with any other condition
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
itemSize, itemSize);
params.leftMargin = (int) (oldParams.leftMargin + x);
params.topMargin = (int) (oldParams.topMargin + y);
gView.rLayout.removeView(item);
putItem(1, params);
gView.rLayout.invalidate();
//it's only ArrayList which contains copies of all Layout's children
//of ImageView class, for providing field moves logic, can't affect
//also recounts how many imageViews are in children of Layout
updateField();
//shows right margins, right amount of items on the field, they are always recounted depending of RelativeLayout children
Log.i("my_log", "Pos change, from " + oldParams.leftMargin + "/" + oldParams.topMargin + " to " + params.leftMargin + "/" + params.topMargin);
Log.i("my_log", "Size of field: " + field.size());
}
});
item.startAnimation(animation);
}
此处使用的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipChildren="false"
android:orientation="vertical" >
<com.crispy_chocolate.outofthebox.GameView
android:id="@+id/game"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipChildren="false" />
</RelativeLayout>
所以问题是 - ImageView 如何在本地动画情况下消失,而在任何其他情况下相同的方法都可以完全正常工作?也许有人会好心帮助我。
最佳答案
显然,这是某种错误,因为如果我将任何 ImageView 放在屏幕的底部,或者写一些文本,动画就会开始完全正常。奇怪。
关于java - 应用 TranslateAnimation 时 ImageView 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25090520/