[英]How can i add animations to existing UI components?
How would Be able to add animations to normal buttons within an android application, I want the button to float from the button of the activity towards the top and then disappear. 如何将动画添加到android应用程序中的普通按钮,我希望该按钮从活动的按钮向顶部浮动,然后消失。
I have looked into the animations library but it seems to me that it is intended for use with external animation that are imported into the project! 我已经看过动画库,但是在我看来,它打算与导入到项目中的外部动画一起使用!
Many thanks! 非常感谢!
Try this: 尝试这个:
button.animate().translationY(value).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
button.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
}).start();
You might need some calculation for the "value", or use translationYBy() with a distance. 您可能需要对“值”进行一些计算,或者使用带有一定距离的translationYBy()。
in you res fodlder create new folder with name anim
create new xml
file there with slide_up.xml
and put this code to it 在您的资源库中,创建一个名称为
anim
新文件夹,并在其中使用slide_up.xml
创建新的xml
文件,并将此代码放入其中
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
then load animation like this 然后像这样加载动画
Animation animSlideUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadein);
txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
animSlideUp = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
// set animation listener
animSlideUp.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// start the animation
txtMessage.startAnimation(animSlideUp);
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for fade in animation
if (animation == animSlideUp) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.