嗨,目前我想创建一个简单的动画,设置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 .. 请帮忙..
答案 0 :(得分:4)
使用ViewPropertyAnimator
或ObjectAnimator
:
<强> 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。