Android 动画 : simultaneous vs. 顺序

标签 android android-animation android-asynctask

我使用了两个图像(img_heart_1img_heart_2)。我有两种动画,一种是平移动画,另一种是缩放 text_anim.xml: animi(Animation),bounce_up.xml :bounce_up_anim(Animation)

  • 顺序:一个接一个的动画。
  • 同时:两个图像动画同时出现。

  • 独家:两个动画(每个图像一个)

  • 包含:只有一个动画(AnimationUtils.loadAnimation(this, R.anim.Same))用于两个图像动画。

结果是连续的和不同的动画,预期的结果。但如果我从 firstAnimationAsync 中剪切 secAnim.execute(); 并放入 onResume,两个动画将同时运行,仅运行一次

如果我将 secAnim.execute() 保留在 onResume() 中,并放置 img_heart_2.startAnimation(animi); 而不是 img_heart_2.startAnimation(bounce_up_anim); 它会同时运行一次。

如果我将 secAnim.execute() 放在 firstAnimationAsynconPostExecute() 中并保持 startAnimation(animi) 对于两个图像,现在,第一个动画将第一次运行,然后第二次运行两个动画。

为什么会这样?

此外,如果同时显示两张图片,第二张看起来有点压抑(垂直向下压缩)。我还放了 da=null,(现在评论了)它不应该使那个动画无效吗?

我还希望我的翻译动画能够坚持到最后,而不是快速返回或变得不可见。

代码:text_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    >
        <translate
            android:fromXDelta="0"
            android:toXDelta="50"
            android:fromYDelta="0"
            android:toYDelta="100"
            android:duration="3000"
            android:fillAfter="false"/>


</set>

代码:bounce_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
        <scale 
            android:fromXScale="1.0"
            android:toXScale="2.0"
            android:fromYScale="1.0"
            android:toYScale="3.0"
            android:pivotX="50%"
            android:pivotY="0%"
            android:duration="3000"/>   
</set>

代码:anidro.java

package my.trials.anidro;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class anidro extends Activity {
    ImageView img_heart_1,img_heart_2, img_heart_3;
    Animation animi, bounce_up_anim;
    Bitmap b1,b2;
    firstAnimationAsync da;
    secondAnimAsync secAnim;
    @Override
    public void onPause(){
        super.onPause();
    }
    @Override
    public void onResume(){
        super.onResume();
        InitializeLayouts();
        **da.execute();**

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    private void InitializeLayouts() {  
        img_heart_1=(ImageView)findViewById(R.id.lay_main_heartImg);
        img_heart_2 = (ImageView)findViewById(R.id.lay_main_heart2Img);
        **bounce_up_anim**=AnimationUtils.loadAnimation(this, R.anim.bounce_up);
        **animi** = AnimationUtils.loadAnimation(this, R.anim.text_anim);
        b1 = BitmapFactory.decodeResource(getResources(),R.drawable.anidro_heart2); 
        b2=BitmapFactory.decodeResource(getResources(), R.drawable.anidro_heart3);
        da= new firstAnimationAsync();
        secAnim = new secondAnimAsync();

        //img_heart_12=(ImageView)findViewById(R.id.lay_main_koalaImg);
    }
    private class firstAnimationAsync extends AsyncTask<Void, Void, Void>{
        protected void onPreExecute(){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            img_heart_1.setImageBitmap(b1);
            img_heart_1.setVisibility(View.VISIBLE);
            img_heart_1.startAnimation(**animi**);
            //img_heart_1.setVisibility(View.INVISIBLE);
        }
        @Override
        protected Void doInBackground(Void...params){
            try{

                Thread.sleep(1800);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void v){
                    **secAnim.execute();**
            //da=null;
            return;
        }
    }
    private class secondAnimAsync extends AsyncTask<Void, Void, Void>{
        protected void onPreExecute(){
            img_heart_2.setImageBitmap(b2);
            //da=null;
            img_heart_2.setVisibility(View.VISIBLE);
            //img_heart_1.setVisibility(View.INVISIBLE);
            img_heart_2.startAnimation(**bounce_up_anim**);
        }

        protected Void doInBackground(Void...params){
            try{
                Thread.sleep(5000);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void v){
            //secAnim = null;
            return;
        }
    }
}

最佳答案

我知道这是一个老问题,但为了将来引用,您可以使用 AnimationSet.Builder

The Builder object is a utility class to facilitate adding animations to a AnimatorSet along with the relationships between the various animations. The intention of the Builder methods, along with the play() method of AnimatorSet is to make it possible to express the dependency relationships of animations in a natural way. Developers can also use the playTogether() and playSequentially() methods if these suit the need, but it might be easier in some situations to express the AnimatorSet of animations in pairs.

For example, this sets up a AnimatorSet to play anim1 and anim2 at the same time, anim3 to play when anim2 finishes, and anim4 to play when anim3 finishes:

AnimatorSet s = new AnimatorSet();
s.play(anim1).with(anim2);
s.play(anim2).before(anim3);
s.play(anim4).after(anim3);

关于Android 动画 : simultaneous vs. 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6908119/

相关文章:

java - 在 android studio 中为具有 spring security 的 spring boot 服务器端创建登录

android - 如何在android中解析复杂的JSON文件

android - 奇怪的选项菜单 - 导航栏中的按钮(Android 4.0)

android - 带有动画的 RecyclerView scrollToPositionWithOffset

java - 如何通过mvvm架构让Room数据库insert方法返回int?

java - 无法使用 asynctask 获取自定义列表适配器来填充 ListView

Android 房间数据库没有立即在数据库中插入数据

java - 选定 Canvas 的 Android 动画

java - 如何共享所有应用程序 Activity 的对象

android - Android使用带有列表 Activity 的JSON强制关闭并显示错误