ami*_*hgc 20 animation android
我在TextView上有两个TranslateAnimations,我希望它们一个接一个地执行.但是,通过使用下面的代码,只执行第二个代码.
我怎么解决这个问题?
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
animation.setDuration(200);
wave.startAnimation(animation);
TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
animation1.setDuration(200);
wave.startAnimation(animation1);
Run Code Online (Sandbox Code Playgroud)
and*_*oot 52
将它们与动画集链接在一起
AnimationSet as = new AnimationSet(true)
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
animation.setDuration(200);
as.addAnimation(animation);
TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
animation1.setDuration(200);
animation1.setStartOffset(200);
as.addAnimation(animation1);
wave.startAnimation(as);
Run Code Online (Sandbox Code Playgroud)
pgs*_*rom 33
编辑: Andy Boots回答下面是更好的答案imo.
只需设置你的第一个就像这样,一旦动画结束,它就会启动另一个:
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
wave.startAnimation(animation1);
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:仅使用当前代码执行第二个动画的原因是因为它会覆盖第一个动画的播放(实际上都是播放,但您只看到最新的动画开始).如果你喜欢我写的,他们将按顺序播放,而不是并行播放.
Kha*_*ear 10
你也可以使用android:startOffset属性通过XML本身来做到这一点,并且有一个问题:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="300"
android:fromXScale="0%"
android:fromYScale="0%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="100%"
android:toYScale="100%" />
<alpha
android:duration="300"
android:fromAlpha="0"
android:toAlpha=".5" />
<alpha
android:duration="300"
android:fromAlpha=".5"
android:startOffset="300"
android:toAlpha="1" />
</set>
Run Code Online (Sandbox Code Playgroud)
还有一种方法可以实现这一目标,当您需要逐个动画大量视图时,这种方法非常有用.您可以使用setStartOffset方法在动画开始前设置延迟.因此,如果您知道,第一个动画结束需要多长时间,您可以将其设置为第二个动画的延迟.这是一个示例,我一个接一个地在它们下面动画六和ImageButtons六TextViews:
public void animateButtons() {
// An array of buttons
int[] imageButtonIds = {R.id.searchButton, R.id.favoriteButton, R.id.responseButton, R.id.articleButton, R.id.resumeButton, R.id.subscribeButton};
// Array of textViews
int[] textViewIds = {R.id.searchTextView, R.id.favoriteTextView, R.id.responseTextView, R.id.articleTextView, R.id.resumeTextView, R.id.subscribeTextView};
int i = 1;
for (int viewId : imageButtonIds) {
ImageButton imageButton = (ImageButton) findViewById(viewId);
// Animation from a file fade.xml in folder res/anim
Animation fadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade);
// Delay for each animation is 100 ms bigger than for previous one
fadeAnimation.setStartOffset(i * 100);
imageButton.startAnimation(fadeAnimation);
// The same animation is for textViews
int textViewId = textViewIds[i-1];
TextView textView = (TextView) findViewById(textViewId);
textView.startAnimation(fadeAnimation);
i ++;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的res/anim文件夹中,我有一个文件,调用fade.xml这些内容:
<?xml version="1.0" encoding="utf-8"?>
<!-- Fade animation with 500 ms duration -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34925 次 |
| 最近记录: |