5

我正在实现一个应用程序,我想在 iPhone 中摇动图标。

我怎样才能实现相同的?

我在这里附上了我的动画代码。请建议我任何解决方案。我已经尝试了已经在堆栈溢出中指定的soln,但它在我的情况下不起作用。而这个类是一个非活动类,这个动画集就是在其中编写的。

protected void animateDragged(){
    View v = getChildAt(dragged);
    int x = getCoorFromIndex(dragged).x + childSize / 2, y = getCoorFromIndex(dragged).y + childSize / 2;
    int l = x - (3 * childSize / 4), t = y - (3 * childSize / 4);
    v.layout(l, t, l + (childSize * 3 / 2), t + (childSize * 3 / 2));
    AnimationSet animSet = new AnimationSet(true);
    ScaleAnimation scale = new ScaleAnimation(.667f, 1, .667f, 1, childSize * 3 / 4, childSize * 3 / 4);
    scale.setDuration(animT);
    AlphaAnimation alpha = new AlphaAnimation(1, .5f);
    alpha.setDuration(animT);

    animSet.addAnimation(scale);
    animSet.addAnimation(alpha);
    animSet.setFillEnabled(true);
    animSet.setFillAfter(true);

    v.clearAnimation();
    v.startAnimation(animSet);
}

谢谢

4

2 回答 2

8

也许你可以shake.xmlanim文件夹中制作。

    <?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fromDegrees="-5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toDegrees="5" />

现在在你的方法animateDragged()中使用它

Animation animation=AnimationUtils.loadAnimation(this,R.anim.shake);
v.startAnimation(animation);
于 2012-10-04T05:52:28.397 回答
1

您还可以通过编程方式为每个视图制作不同的随机动画。只需在每个图标的循环中使用下面的代码片段

        float px = ((float) Math.random() * .5f);
        float py = ((float) Math.random() * .5f);

        RotateAnimation anim = new RotateAnimation((float) Math.PI / 2 * -1.f, (float) Math.PI / 2,
                RotateAnimation.RELATIVE_TO_SELF, .25f + px,
                RotateAnimation.RELATIVE_TO_SELF, .25f + py);
        anim.setDuration((long) (80. + (Math.random() * 50.)));
        anim.setRepeatCount(RotateAnimation.INFINITE);
        anim.setRepeatMode(RotateAnimation.REVERSE);

        v.startAnimation(anim);
于 2017-01-10T15:49:10.530 回答