Applying Animations on Shapes

Skip to end of metadata
Go to start of metadata
Animation is one of the most important parts of the presentations that make them more attractive and meaningful. Aspose.Slides for Java also allows developers to apply different kinds of animation effects on different kinds of shapes. There are more than 60 pre-defined aniamtion effects provided by Aspose.Slides for Java . In this topic, we will practice how we can apply animation effects on shapes and also control their animation order.

Applying Animation Effects

To apply an animation effect on any shape in the slide, please follow the steps below:

  • Create an instance of Presentation class
  • Obtain the reference of a slide by using its Position
  • Add a Shape to the slide
  • Apply a desired Animation Effect on the shape from the list of pre-defined effects. More than 60 animation effects are pre-defined in Aspose.Slides for Java
  • Write the modified presentation as a PPT file


In the example given below, we have added a rectangle shape to the slide with the Spiral effect applied on it.

Example

[Java]
 try
 {
    //Instantiate a Presentation object that represents a PPT file        
    Presentation pres = new Presentation(new FileInputStream(new File("demo.ppt")));


    //Accessing a slide using its slide position
    Slide slide = pres.getSlideByPosition(2);


    //Adding a rectangle shape into the slide by defining its X,Y postion, width
    //and height
    Shape shape=slide.getShapes().addRectangle(1400,1100,3000,2000);


    //Applying an animation effect on the rectangle shape
    shape.getAnimationSettings().setEntryEffect(ShapeEntryEffect.SPIRAL);


    //Writing the presentation as a PPT file
    pres.write(new FileOutputStream(new File("modified.ppt")));
 }
 catch(Exception ex)
 {
    System.out.println(ex.toString());
 }

 

[PHP]
  <?php

   //Using aspose.slides.jar file so that the classes inside the jar file
   //can be used
   java_require("aspose.slides.jar");


   try
   {
     //Creating a file input stream to read the PPT file
     $fistream=new Java("java.io.FileInputStream","C:\\demo.ppt");


     //Instantiate a Presentation object that represents a PPT file
     $pres=new Java("com.aspose.slides.Presentation",$fistream);


     //Accessing a slide using its slide position
     $slide=$pres->getSlideByPosition(2);     


     //Adding a rectangle shape into the slide by defining its X,Y postion,
     //width and height
     $shape=$slide->getShapes()->addRectangle(1400,1100,3000,2000);


     //Creating ShapeEntryEffect enumeration so that it can be used later
     $ShapeEntryEffect=new Java("com.aspose.slides.ShapeEntryEffect");


     //Applying an animation effect on the rectangle shape
     $shape->getAnimationSettings()->setEntryEffect($ShapeEntryEffect->SPIRAL);


     //Creating a file output stream to write the output file
     $fostream=new Java("java.io.FileOutputStream","C:\\modified.ppt");


     //Writing the presentation as a PPT file
     $pres->write($fostream);


     //Closing the streams
     $fistream->close();
     $fostream->close();


   }
   catch(JavaException $ex)
   {
        echo $ex->toString();        
   }

  ?>

 

The above code snippet adds a rectangle to the slide with the Spiral animation effect applied on it as shown below:



Figure: Spiral animation effect applied on a rectangle

Controlling Animation Order

The above section describes about applying animation effect on a single shape. But, if you want to add more than one shapes and also want to apply aniamtion effects on these shapes then you may want to change the animation order of the shapes added to the slide. For example, in the normal scenario, if you add two shapes: Shape A & Shape B then first Shape A will animate and then Shape B . But, if you want to specify that Shape A should animate after Shape B then you can change their animation order.


To control the animation order of desired shapes in the slide, please follow the steps below:

  • Create an instance of Presentation class
  • Obtain the reference of a slide by using its Position
  • Add multiple Shapes to the slide
  • Apply desired Animation Effects on these shapes
  • Set the Animation Order of these shapes
  • Write the modified presentation as a PPT file


In the example given below, we have added rectangle & ellipse shapes to the slide. And then changed their animation order after applying animation effects on both shapes.

Example

[Java]
 try
 {
    //Instantiate a Presentation object that represents a PPT file        
    Presentation pres = new Presentation(new FileInputStream(new File("demo.ppt")));


    //Accessing a slide using its slide position
    Slide slide = pres.getSlideByPosition(2);    


    //Adding two shapes to the slide
    Shape shape1=slide.getShapes().addRectangle(1400,1100,3000,2000);
    Shape shape2=slide.getShapes().addEllipse(2400,1150,1000,1900);


    //Applying animation effects on both shapes
    shape1.getAnimationSettings().setEntryEffect(ShapeEntryEffect.SPIRAL);
    shape2.getAnimationSettings().setEntryEffect(ShapeEntryEffect.BOX_OUT);


    //Setting the animation order for both shapes. According to below order,
    //shape2 will animate first and then the shape1
    shape1.getAnimationSettings().setAnimationOrder(2);
    shape2.getAnimationSettings().setAnimationOrder(1);    


    //Writing the presentation as a PPT file
    pres.write(new FileOutputStream(new File("modified.ppt")));
 }
 catch(Exception ex)
 {
    System.out.println(ex.toString());
 }

 

[PHP]
  <?php

   //Using aspose.slides.jar file so that the classes inside the jar file
   //can be used
   java_require("aspose.slides.jar");


   try
   {
     //Creating a file input stream to read the PPT file
     $fistream=new Java("java.io.FileInputStream","C:\\demo.ppt");


     //Instantiate a Presentation object that represents a PPT file
     $pres=new Java("com.aspose.slides.Presentation",$fistream);


     //Accessing a slide using its slide position
     $slide=$pres->getSlideByPosition(2);     


     //Adding two shapes to the slide
     $shape1=$slide->getShapes()->addRectangle(1400,1100,3000,2000);
     $shape2=$slide->getShapes()->addEllipse(2400,1150,1000,1900);


     //Creating ShapeEntryEffect enumeration so that it can be used later
     $ShapeEntryEffect=new Java("com.aspose.slides.ShapeEntryEffect");


     //Applying animation effects on both shapes
     $shape1->getAnimationSettings()->setEntryEffect($ShapeEntryEffect->SPIRAL);
     $shape2->getAnimationSettings()->setEntryEffect($ShapeEntryEffect->BOX_OUT);


     //Setting the animation order for both shapes. According to below order,
     //shape2 will animate first and then the shape1
     $shape1->getAnimationSettings()->setAnimationOrder(2);
     $shape2->getAnimationSettings()->setAnimationOrder(1);


     //Creating a file output stream to write the output file
     $fostream=new Java("java.io.FileOutputStream","C:\\modified.ppt");


     //Writing the presentation as a PPT file
     $pres->write($fostream);


     //Closing the streams
     $fistream->close();
     $fostream->close();


   }
   catch(JavaException $ex)
   {
        echo $ex->toString();        
   }

  ?>

 

The above code snippet adds a rectangle and an ellipse to the slide with some animation effects applied on them as shown below:


Figure: Controlling Animation Order of shapes

The above image is static and is not showing the animation effect. For seeing the animation effect produced by the above code.
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.