android - 如何用Android做交互式动画(翻译)

标签 android android-animation

我在 Android 中有一些 png 序列,我需要将它们的 x 和 y 位置转换为从屏幕顶部到底部的动画。在动画发生时,我需要对象接收点击事件。

我知道这在 3.0 之前的 Android 版本中效果不佳,因为对象的显示 与实际对象位于不同的位置,因此您不会在中间获得点击事件- 动画(这是我需要的),因为您没有点击对象,只是在显示器上点击。

我正在做这样的事情,动画看起来不错,但我从来没有进入点击处理程序:

Animation animation = new TranslateAnimation(0, 20,0, 300);
animation.setDuration(1000);
ticket.startAnimation(animation);

ticket.startAnimation(animation);

ticket.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
            //I don't get into this click event during animation
        view.setVisibility(View.GONE);
    }
});

为 3.0 之前的设备支持交互式动画的解决方法是什么?是否有自定义动画类将使用后台线程为对象的 x 和 y 位置设置动画?

--更新--- 我尝试使用 NineOldAndroid 的解决方案,动画看起来不错,但在动画期间我没有收到 onclick 事件。这是我更新的代码。所以这在 ICS 上工作正常,但在 2.3 上不行 我做错了什么吗?还是 NineOldWorld 的解决方案实际上并没有改变引擎盖下的布局边距?

ImageView ticket = new ImageView(myActivity);
            ticket.setImageResource(R.drawable.ticket);
            ticket.setVisibility(View.INVISIBLE);
            ticketHolder.addView(ticket);
            ValueAnimator animation = ObjectAnimator.ofFloat(ticket, "y", 
                    -200f, ticketHolder.getHeight());
            animation.setDuration(3000);
            animation.start();
            animation.setStartDelay(i*1000);
            animationsMap.put(animation, ticket);

            ticket.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    view.setVisibility(View.GONE);
                }
            });

            animation.addListener(new AnimatorListenerAdapter() {
                public void onAnimationEnd(Animator animation) {
//                    tickets.remove(((ObjectAnimator)animation).getTarget());
                    ImageView t = (ImageView) ((ObjectAnimator) animation).getTarget();
                    t.setVisibility(View.GONE);
                    animationsMap.remove(animation);
                    if(animationsMap.size() == 0){
                        ticketsAnimating = false;
                        scrim.setVisibility(View.VISIBLE);
                    }

                }
                @Override
                public void onAnimationStart(Animator animation){
                    ImageView t = (ImageView) ((ObjectAnimator) animation).getTarget();
                    t.setVisibility(View.VISIBLE);
                }
            });

-- 更新 #2 --- 我也尝试了路径动画,但在动画期间和之后,我没有收到按钮单击事件。有人知道我做错了什么吗?

mButton = (Button) findViewById(R.id.delete_me);
mButtonProxy = AnimatorProxy.wrap(mButton);

// Set up the path we're animating along
AnimatorPath path = new AnimatorPath();
path.moveTo(0, 0);
path.lineTo(0, 300);
path.curveTo(100, 0, 300, 900, 400, 500);

// Set up the animation
final ObjectAnimator anim = ObjectAnimator.ofObject(this, "buttonLoc",
        new PathEvaluator(), path.getPoints().toArray());
anim.setDuration(10000);

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("united", "button clicked");
        anim.start();
    }
});

最佳答案

你是对的,问题是在 Honeycomb 之前,只有动画 View 的视觉方面正在发生。

这意味着除了做“视觉”动画之外,您还必须设置/动画动画 View 的框架/LayoutParams。

对于 Honeycomb 之前的设备,我找到了 http://nineoldandroids.com/对于收听动画非常有用,您可以使用它来适本地设置 View 的 LayoutParams,但还有其他示例,例如 SO 上的这个:https://stackoverflow.com/a/3952933/429047

关于android - 如何用Android做交互式动画(翻译),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9595881/

相关文章:

android - 键盘打开时动 Canvas 局调整大小

android - 拖放在 nhaarman 的 ListviewAnimation 库中不起作用

java - Android ImageView 动画很慢

android - 内存不足错误

Android OpenGL 纹理 : creating and deleting them on the fly

java - Realm.io - 在 Android 之外使用(在 Java 中)

javascript - 修改 Cordova 插件加载行为以加快加载时间

android - 动画(缩放和平移)android Canvas View 上的一部分图像

安卓 : Hide RelativeLayout with animationwhen scroll down/up listview

android - Google Maps Android API v2 中带有文本的 map 标记