在 android 中添加和动画化 imageView 时的奇怪行为
Strange behaviour when adding and animating imageViews in android
我有一个带有背景位图的 LinearLayout。我正在使用动画向此布局添加 2-3 个图像。问题是:每次我添加图像时,第一个图像动画从左上角开始,对于紧随该动画之后的所有其他图像,它从中心开始工作得很好。我为这个烦人的小问题苦苦挣扎了好几天。您有什么建议会导致这种情况吗?
这是我制作的 gif 中的问题:http://imgur.com/FCPgof1
我的动画:`
<scale
android:duration="750"
android:fillAfter="false"
android:fromXScale="0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
`
我的布局:
<LinearLayout
android:id="@+id/layoutCompareUppoints"
android:layout_width="140dp"
android:layout_height="28dp"
android:background="@drawable/uppballsbgnew" >
</LinearLayout>
我的代码:
`private void animUppointsBefore() {
if (k < Integer.valueOf(uppoints)) {
Animation a = AnimationUtils.loadAnimation(this,
R.anim.debug_grow_fast_animation);
ImageView upp = new ImageView(this);
upp.setImageResource(R.drawable.uppballlightnew);
upp.setAdjustViewBounds(true);
uppLayout.addView(upp);
upp.startAnimation(a);
a.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation a) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation a) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation a) {
// TODO Auto-generated method stub
k++;
animUppointsBefore();
}
});
}
}`
您最有可能看到的问题是图像在任何动画实际发生之前被添加到布局中。图片将从全尺寸开始,缩小到 0,然后放大到 1。
您要做的是在添加之前使其不可见,然后使其显示在屏幕之外。您可以使用几种方法,通常的方法是将 ImageView
设置为 View#INVISIBLE
,然后使用 Animation#AnimationListener
,在动画开始时将视图设置为 View#VISIBLE
。
如果可以,使用 Animators 会更容易(支持库中有一些库不允许这样做)。您可以在添加之前将视图的 Alpha 设置为 0,然后作为动画的一部分立即将其设置为 1。只需制作两个 ObjectAnimators
,一个用于 alpha,一个用于缩放,然后将它们组合成一个 AnimatorSet
.
编辑:
将评论中的最终答案放在这里
与其在动画之前添加视图,不如将已经在布局中布置但不可见的图像视图放在一起。这样,就不需要新的布局和测量通道,因为它们可能会把测量结果搞砸到枢轴点。
我有一个带有背景位图的 LinearLayout。我正在使用动画向此布局添加 2-3 个图像。问题是:每次我添加图像时,第一个图像动画从左上角开始,对于紧随该动画之后的所有其他图像,它从中心开始工作得很好。我为这个烦人的小问题苦苦挣扎了好几天。您有什么建议会导致这种情况吗?
这是我制作的 gif 中的问题:http://imgur.com/FCPgof1
我的动画:`
<scale
android:duration="750"
android:fillAfter="false"
android:fromXScale="0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
`
我的布局:
<LinearLayout
android:id="@+id/layoutCompareUppoints"
android:layout_width="140dp"
android:layout_height="28dp"
android:background="@drawable/uppballsbgnew" >
</LinearLayout>
我的代码: `private void animUppointsBefore() {
if (k < Integer.valueOf(uppoints)) {
Animation a = AnimationUtils.loadAnimation(this,
R.anim.debug_grow_fast_animation);
ImageView upp = new ImageView(this);
upp.setImageResource(R.drawable.uppballlightnew);
upp.setAdjustViewBounds(true);
uppLayout.addView(upp);
upp.startAnimation(a);
a.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation a) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation a) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation a) {
// TODO Auto-generated method stub
k++;
animUppointsBefore();
}
});
}
}`
您最有可能看到的问题是图像在任何动画实际发生之前被添加到布局中。图片将从全尺寸开始,缩小到 0,然后放大到 1。
您要做的是在添加之前使其不可见,然后使其显示在屏幕之外。您可以使用几种方法,通常的方法是将 ImageView
设置为 View#INVISIBLE
,然后使用 Animation#AnimationListener
,在动画开始时将视图设置为 View#VISIBLE
。
如果可以,使用 Animators 会更容易(支持库中有一些库不允许这样做)。您可以在添加之前将视图的 Alpha 设置为 0,然后作为动画的一部分立即将其设置为 1。只需制作两个 ObjectAnimators
,一个用于 alpha,一个用于缩放,然后将它们组合成一个 AnimatorSet
.
编辑:
将评论中的最终答案放在这里
与其在动画之前添加视图,不如将已经在布局中布置但不可见的图像视图放在一起。这样,就不需要新的布局和测量通道,因为它们可能会把测量结果搞砸到枢轴点。