动画儿童回到原来的位置

时间:2013-09-27 10:44:37

标签: android animation android-animation

我使用animate()方法使用以下代码将TextView发送到布局的最右下方:

move.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                int xValue = container.getWidth() - myView.getWidth();
                int yValue = container.getHeight() - myView.getHeight();
                myView.animate().x(xValue).y(yValue);
            }
        });

这样做的伎俩,但我有点失去了将元素恢复到原来的位置。我确实尝试了以下方法,但它不起作用:

public class MainActivity extends Activity {

    Button fadeIn, fadeOut, move, moveBack;
    TextView myView;
    LinearLayout container;
    float originalX , originalY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fadeIn = (Button)findViewById(R.id.fadeIn);
        fadeOut = (Button)findViewById(R.id.fadeOut);
        move = (Button)findViewById(R.id.move);
        moveBack = (Button)findViewById(R.id.moveBack);
        myView = (TextView)findViewById(R.id.myView);
        originalX = myView.getX();
        originalY = myView.getY();
        Log.i("X and Y", ""+originalX+" "+originalY);
        container = (LinearLayout) findViewById(R.id.container);
        fadeIn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                myView.animate().alpha(1);
            }
        });
        fadeOut.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                myView.animate().alpha(0);
            }
        });
        move.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                int xValue = container.getWidth() - myView.getWidth();
                int yValue = container.getHeight() - myView.getHeight();
                myView.animate().x(xValue).y(yValue);
            }
        });
        moveBack.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                myView.animate().x(originalX).y(originalY);;
            }

        });

    }
}

以下内容为我提供0,0 X and Y位置。请帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用TranslateAnimations。像.-

之类的东西
TranslateAnimation anim = new TranslateAnimation(originalX, xValue, originalY, yValue);
anim.setFillAfter(true);
anim.setFillEnabled(true);
anim.setDuration(ANIM_DURATION);
myView.startAnimation(anim);

还原值以恢复原始位置.-

TranslateAnimation anim = new TranslateAnimation(xValue, originalX, yValue, originalY);
anim.setFillAfter(true);
anim.setFillEnabled(true);
anim.setDuration(ANIM_DURATION);
myView.startAnimation(anim);