如何在动画中获取ImageView的位置?

时间:2015-01-22 15:39:43

标签: android android-animation android-imageview

当我点击球时动画开始。

它再向下移动到顶部再到中间。

然而,第二次onClick in ball只有在中间点击才有效,这意味着球在身体上移动但功能仍在中间。

如何根据动画设置其位置?

(第二次点击将取消动画)

onClick:

public void ballChange(View v) 
{

    isClicked = false;

    if (ballstarted == false)

    {
        L1_animation = AnimationUtils.loadAnimation(this, R.anim.mercury_anim);
        L1_Ball2.startAnimation(L1_animation);


         ct = new CountDownTimer(4000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished) 
            {

            }

            @Override
            public void onFinish() 
            {


                if (isClicked == false)
                {
                    lifeline-=1;


                    if (lifeline == 0) 
                    {
                        Toast.makeText(getApplicationContext(), "Gameover dude",
                        Toast.LENGTH_LONG).show();
                        heart.setText(""+lifeline);

                        GameOverDialog wp = new GameOverDialog(getApplicationContext());
                        wp.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                        wp.show();
                        wp.setCancelable(false);
                        Mercury_L1.L1_Ball.clearAnimation();
                        Mercury_L1.L1_countDownTimer.cancel();
                        Mercury_L1.timerHasStarted = false;
                        Mercury_L1.isCancelled = true;


                    }
                    else 
                    {   

                        Toast.makeText(getApplicationContext(), String.valueOf(lifeline), Toast.LENGTH_SHORT).show();
                        heart.setText(""+lifeline);
                        minuslife.setVisibility(View.VISIBLE); 

                        Handler handler = new Handler();
                            handler.postDelayed(new Runnable() 
                            {
                               @Override
                               public void run() 
                               {
                                   minuslife.setVisibility(View.GONE); 
                               }
                            }, 500);

                        Mercury_L1.this.v.vibrate(500);

                    }
                }

            }
        }.start();
        ballstarted = true;
        Toast.makeText(getApplicationContext(), String.valueOf(ballstarted), Toast.LENGTH_SHORT).show();
    }

    else
    {

        L1_Ball2.clearAnimation();

        ballstarted = false;
    }

}

1 个答案:

答案 0 :(得分:1)

您正在进行视图动画,该动画仅更新球的绘制位置而不是实际位置。你应该使用像objectanimator这样的属性动画。下面是一些有关如何使用XML的示例代码。

public void doObjectAnimatorXML(){
    AnimatorSet object = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(),R.animator.property_animator);
    object.addListener(new AnimatorListenerAdapter(){
        @Override
        public void onAnimationEnd(Animator animation) {
            simpleLock= false;
        }
    });
    object.setInterpolator(new LinearInterpolator());
    object.setTarget(mLittleChef); //mLittleChef is the view
    object.start();
}

和XML

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"  >
    <objectAnimator 
        android:propertyName="x"
        android:duration="1000"
        android:valueTo="138"
        android:valueType="floatType"/>
    <objectAnimator
        android:propertyName="x"
        android:duration="1000"
        android:valueTo="276"
        android:valueType="floatType"/>
</set>