怎么办交互动画(翻译)与Android动画、Android

2023-09-05 04:52:18 作者:仰起头不流泪@

我在Android的一些PNG序列,我需要从顶到屏幕底部动漫的x和y现在的位置平移。而动画正在发生,我需要的对象来接收click事件。

据我所知3.0之前的Andr​​oid版本,这并不工作非常好,因为显示物体的的是在不同的地方不是实际的对象,所以你没有得到的单击事件中端 - 动画(这正是我所需要的),因为你没有点击的对象,仅在显示屏。

我在做这样的事情,和动画看起来不错,但我从来没有进入click处理函数:

 动画动漫=新TranslateAnimation(0,20,0,300);
animation.setDuration(1000);
ticket.startAnimation(动画);

ticket.startAnimation(动画);

ticket.setOnClickListener(新OnClickListener(){
    公共无效的onClick(视图查看){
            //我不进入动画在此单击事件
        view.setVisibility(View.GONE);
    }
});
 

解决办法:3.0之前,以支持设备的交互动画?是否有一个自定义动画类在那里,将使用一个后台线程动画对象的x和y位置?

- 更新 - 我尝试使用NineOldAndroid的解决方案,和动画看起来不错,但我没有收到动画中的onclick事件。这是我的更新code。因此,这正常工作的ICS,但不能在2.3难道我做错了什么?或者是NineOldWorld的解决方案实际上没有更改布局利润率引擎盖下?

  ImageView的门票=新ImageView的(myActivity);
            ticket.setImageResource(R.drawable.ticket);
            ticket.setVisibility(View.INVISIBLE);
            ticketHolder.addView(票);
            ValueAnimator动画= ObjectAnimator.ofFloat(门票,Y,
                    -200f,ticketHolder.getHeight());
            animation.setDuration(3000);
            animation.start();
            animation.setStartDelay(I * 1000);
            animationsMap.put(动画,门票);

            ticket.setOnClickListener(新OnClickListener(){
                公共无效的onClick(视图查看){
                    view.setVisibility(View.GONE);
                }
            });

            animation.addListener(新AnimatorListenerAdapter(){
                公共无效onAnimationEnd(动画动画){
// tickets.remove(((ObjectAnimator)动画).getTarget());
                    ImageView的T =(ImageView的)((ObjectAnimator)动画).getTarget();
                    t.se​​tVisibility(View.GONE);
                    animationsMap.remove(动画);
                    如果(animationsMap.size()== 0){
                        ticketsAnimating = FALSE;
                        scrim.setVisibility(View.VISIBLE);
                    }

                }
                @覆盖
                公共无效onAnimationStart(动画动画){
                    ImageView的T =(ImageView的)((ObjectAnimator)动画).getTarget();
                    t.se​​tVisibility(View.VISIBLE);
                }
            });
 

- 更新#2 --- 我也试过路径动画,但动画中,后,我没有收到按钮的点击事件。任何人都知道我在做什么错了?

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

//设置我们的动画沿路径
AnimatorPath路径=新AnimatorPath();
path.moveTo(0,0);
path.lineTo(0,300);
path.curveTo(100,0,300,900,400,500);

//设置动画
最后ObjectAnimator动画= ObjectAnimator.ofObject(这一点,buttonLoc
        新PathEvaluator(),path.getPoints()的toArray())。
anim.setDuration(10000);

mButton.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        Log.d(团结,按钮,点击);
        anim.start();
    }
});
 
如何学习 Android Animation

解决方案

您说的没错,问题是,pre-蜂窝,不仅是视觉的动画视图方面正在发生。

这意味着,除了做视觉的动画,你还必须设置/动画帧/动画视图的LayoutParams。

有关pre-蜂窝设备,我发现 http://nineoldandroids.com/ 聆听非常有用为动画,您可以使用设置适当的视图的LayoutParams,但也有其他的例子还有,像这样一个在SO:的 http://stackoverflow.com/a/3952933/429047

I have some png sequences in Android, that I need to animation their x and y postion translation from the top to the bottom of the screen. While the animation is occurring, I need the objects to receive click events.

I understand this doesn't work really well in Android version before 3.0, since the display of the object is in a different place than the actual object so you don't get the click events mid-animation (which is what I need) since you aren't clicking on the object, only on the display.

I'm doing something like this, and the animation looks fine, but I never get into the click handler:

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);
    }
});

What is the workaround to support interactive animations for devices before 3.0? Is there a custom animation class out there that will use a background thread to animate the x and y position of an object?

--update --- I tried using NineOldAndroid's solution, and the animation looks fine, but I'm not getting the onclick events during the animation. Here is my updated code. So this works fine on ICS, but not on 2.3 Am I doing something wrong? or is the NineOldWorld's solution not actually changing the layout margins under the hood?

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);
                }
            });

-- Update #2 --- I also tried the path animation, but during the animation, and after, I'm not getting button click events. Anyone know what I'm doing wrong?

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();
    }
});

解决方案

You're right, the issue is that pre-Honeycomb, only the visual aspect of your animating view is occurring.

This means that in addition to doing the "visual" animation, you also have to set/animate the frame/LayoutParams of the animated view.

For pre-Honeycomb devices, I find http://nineoldandroids.com/ very useful for listening to an animation which you can use for setting the LayoutParams of the view appropriately, but there are other examples as well, such as this one on SO: http://stackoverflow.com/a/3952933/429047