Android TextView简单动画SetX

时间:2014-04-10 00:33:12

标签: java android animation textview

嗨,目前我想创建一个简单的动画,设置TextView的X位置有延迟..这是我的代码..

public class MainActivity extends Activity {

    float ctr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ctr = 0;
        final TextView p1 = (TextView) findViewById(R.id.p1);

        while (ctr <= 100) {
            p1.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    p1.setX(ctr);
                    ctr++;
                }
            }, 5000);
        }
    }

}

目前这段代码只延迟了5秒然后立即定位Textview的x位置,想要我想慢慢定位TextView .. 请帮忙..

1 个答案:

答案 0 :(得分:4)

使用ViewPropertyAnimatorObjectAnimator

<强> ViewPropertyAnimator

float ctr = 0f;
while (ctr <= 100f) {
    p1.animate().x(ctr).setDuration(5000).start();
    ctr++;
}

<强> ObjectAnimator

final ObjectAnimator oa = ObjectAnimator.ofFloat(p1, "x", 100f);
oa.setDuration(5000);
oa.start();

要在3.0以下的设备(Honeycomb)上使用动画,请查看NineOldAndroids