Domanda

I want to get multiple animations of an imageview. Like i want one image to fall from the top of the screen and before that image completes the animation the next animation should have already started from the top of the screen .I am able to make the image fall from the top but cant do the above feature. Right now when the image reaches the end only then the next animation starts.Please help

È stato utile?

Soluzione

If you know the time of your first animation, in its onAnimationStart you can use something as below to start your next animation before the current animation is done:

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //add here your animation to start
            }
        }, 3000);//change the timer in milliseconds

In this case, your animation will start 3seconds after. Change this value to fit with your needs.

Altri suggerimenti

 public class FliperViewExampleActivity extends Activity implements OnGestureListener {
 /** Called when the activity is first created. */
private int imageID[] = {R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.ic_launcher};
private ViewFlipper viewFlipper = null;  
 private GestureDetector gestureDetector = null;
 private Button btn_next,btn_pre;
private int n;
int i=1;
private Timer timer;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn_next = (Button)findViewById(R.id.btn_next);
    btn_pre = (Button)findViewById(R.id.btn_pre);
    timer = new Timer();;
    btn_next.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(i==4)
            {


            }
            else
            {
            nextImage();
            }
        }
    });

    btn_pre.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(i==1)
            {


            }
            else
            {
            previousImage();
            }
        }
    });

    viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper); 
    // gestureDetector Object is used to detect gesture events
    gestureDetector = new GestureDetector(this); 
    n=imageID.length;

    for (int i = 0; i < imageID.length; i++)  
    { 
        ImageView image = new ImageView(this);  
        image.setImageResource(imageID[i]);  
        image.setScaleType(ImageView.ScaleType.FIT_XY);
        viewFlipper.addView(image, new LayoutParams(  
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }

    //pageSwitcher(3);
  }




public void pageSwitcher(int seconds) {
     // At this line a new Thread will be created
    timer.schedule(new RemindTask(), 4000, seconds * 1000);     // delay
                                                                   // in
    // milliseconds
}

// this is an inner class...
class RemindTask extends TimerTask {

    @Override
    public void run() {

        // As the TimerTask run on a seprate thread from UI thread we have
        // to call runOnUiThread to do work on UI thread.
        runOnUiThread(new Runnable() {
            public void run() {
                  if(i==n)
                  {
                     timer.cancel(); 


                  }
                  else
                  {
                      nextImage();
                  }



            }
        });

    }
}
 private void nextImage(){
    System.out.println("VALUE OF I IS"+i);
    System.out.println("VALUE OF N IS"+n);

         if(i==1)
         {
                /* 1.RotateAnimation rotateAnimation = new RotateAnimation(0, 360,       Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
             rotateAnimation.setDuration(1000);  
             rotateAnimation.setRepeatCount(0);  */
             //start animation 

               /*2. AlphaAnimation  blinkanimation= new AlphaAnimation(1, 0); // Change alpha from fully   visible to invisible
             blinkanimation.setDuration(300); // duration - half a second
             blinkanimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
             blinkanimation.setRepeatCount(1); // Repeat animation infinitely
             blinkanimation.setRepeatMode(Animation.ZORDER_NORMAL);*/

             Toast.makeText(FliperViewExampleActivity.this,"Test Image",Toast.LENGTH_SHORT).show();
             ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.loadAnimator(FliperViewExampleActivity.this, R.anim.flipper); 
             anim.setTarget(this.viewFlipper);
             anim.setDuration(1000);
             anim.start();

            /*3. TranslateAnimation _tAnim = new TranslateAnimation(0,0, -500, 0);
                _tAnim.setInterpolator(new AnticipateOvershootInterpolator(5));
                _tAnim.setDuration(1000);*/

                                   /*4. Animation shake =   AnimationUtils.loadAnimation(FliperViewExampleActivity.this,R.anim.wobble);
             shake.reset();
             shake.setFillAfter(true);
             */

                /*5. int fadeInDuration = 1000; // Configure time values here
                Animation fadeIn = new AlphaAnimation(0, 1);
                fadeIn.setInterpolator(new AccelerateInterpolator()); // add this
                fadeIn.setDuration(fadeInDuration);
     */


             //  this.viewFlipper.setInAnimation(_tAnim);


             /*this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,  
                        R.anim.zoomin));  */
            // this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
                       // R.anim.zoomout));  
             this.viewFlipper.showNext();
         }
         else
         {
    this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,  
            R.anim.push_left_in));  
    this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
            R.anim.push_right_out));  
    this.viewFlipper.showNext(); 
         }

    i++;
  //  pageSwitcher(3);
     }


private void previousImage(){
    System.out.println("VALUE OF I IS"+i);
    System.out.println("VALUE OF N IS"+n);
    if(i==2)
     {

         this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,  
                    R.anim.zoomin));  
         this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
                    R.anim.push_left_out));  
         this.viewFlipper.showPrevious();
     }
    else
    {
    this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,  
            R.anim.push_right_in));  
    this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
            R.anim.push_left_out)); 
    this.viewFlipper.showPrevious();  
    }
    i--;






}

@Override
public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    if (e1.getX() - e2.getX() > 60)  
    {  
         nextImage();
        return true;  
    }
    else if (e1.getX() - e2.getX() < -60)  
    {  
            previousImage();
            return true;
    }  
    return true;
}
@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}
@Override
public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

@Override 
public boolean onTouchEvent(MotionEvent event)  
{  
    return gestureDetector.onTouchEvent(event);  
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top