我已经关注了这个答案How to make a Button randomly Move ,它按我想要的方式工作,但按钮出现并从屏幕上消失。
我想这样做:h ttp://jsfiddle.net/Xw29r/15/
我在android中没有找到任何与此相关的内容。
最佳答案
使用 set
和 translate
创建 XML
动画,并将此 XML
文件放入 /res/anim
文件夹。
这是一个例子
//sequestial_move.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<!-- Use startOffset to give delay between animations -->
<!-- Move -->
<translate
android:duration="800"
android:fillAfter="true"
android:fromXDelta="0%p"
android:startOffset="300"
android:toXDelta="75%p" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromYDelta="0%p"
android:startOffset="1100"
android:toYDelta="70%p" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromXDelta="0%p"
android:startOffset="1900"
android:toXDelta="-75%p" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromYDelta="0%p"
android:startOffset="2700"
android:toYDelta="-70%p" />
</set>
使用:
从XML
加载动画并使用.startAnimation(..)
方法使用动画
............
...................
TextView txtMessage = (TextView) findViewById(R.id.txtMessage);
Button btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
Animation animSequestialMove = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.sequestial_move);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// start the animation
txtMessage.startAnimation(animSequestialMove);
}
});
.............
.....................
这是一个很好的tutorial关于不同类型的动画
。
希望对你有帮助~
关于java - 在屏幕中平滑移动按钮 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44101107/