开发者

Android Animations : simultaneous vs. sequential

I am using two Images (img_heart_1 and img_heart_2). I have two animations, one translatory and other scaling text_anim.xml: animi(Animation),bounce_up.xml :bounce_up_anim(Animation)

  • Sequential : one animation after other.
  • simultaneous: both image animations at once.

  • Exclusive: two animations (one for each image)

  • Inclusive : only one animation(AnimationUtils.loadAnimation(this, R.anim.Same)) used for both image-animations.

The result is sequential and different animations, expected result. but if I cut secAnim.execute(); from firstAnimationAsync and put in onResume, both animations will run simultaneously, once only

If I keep secAnim.execute() in onResume(), and put img_heart_2.startAnimation(animi); instead of img_heart_2.startAnimation(bounce_up_anim); it will run once , simultaneously.

If I put secAnim.execute() in onPostExecute() of firstAnimationAsync and keep startAnimation(animi) for both images, now, first animation will run first time then, second time both animations will run.

Why is this so?

Also, if two images are going on simultaneously, second one looks a little bit pressed (compressed vertically towards bottom). I have also put da=null, (now commented) shouldn't it nullify that animation?

I also want my translatory animation to stick to end not snap back or become invisible.

code: 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>

code : 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>

code : 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开发者_如何学JAVA;
    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;
        }
    }
}


I know this is an old question, but for future reference, you can use an 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);


You really shouldn't use ASyncTask for graphical things. No one can predict exactly when tasks will be executed in the system, so it's a very bad idea.

Why don't you use Animation Listener instead ? You just need to create your first animation, and add a new animation listener that will launch your second animation on the start or the end of the first.

For nullify animation, just call

img_heart.clearAnimation();

And for the stick to end, I'm not sure what you want, but it's probably that you want your image to stay where it is at the end of the animation, so, you need to set this attribute in the animation declaration :

android:fillAfter="true"

android:fillBefore="false"
android:fillEnabled="true"

Only fillAfter seems to be mandatory, so try it first, but if it don't work, add the next two lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜