Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java to add animation to PPT

Shulou Source: shulou.com Published: 2022-06-01 13:18:08 10月03日 Update

This article focuses on "how to use Java to add animation to PPT", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Java to add animation to PPT.

The test environment includes:

Target test document: Power Point 2013

Compilation environment: IntelliJ IDEA 2018

JDK version: 1.8.0

PPT Library version: spire.presentation.jar 4.3.2

Note: when you add animation types (AnimationEffectType) through this PPT library, you can add about 150 different types.

Java program code

1. Add preset animation effects

a. Create new PPT documents, add shapes, and animate effects

Import com.spire.presentation.*;import com.spire.presentation.drawing.FillFormatType;import com.spire.presentation.drawing.animation.AnimationEffectType;import java.awt.*;import java.awt.geom.Rectangle2D;public class AddAnimationToShape {public static void main (String [] args) throws Exception {/ / create PowerPoint document Presentation ppt = new Presentation (); / / get slide ISlide slide = ppt.getSlides () .get (0) / / add a shape to the slide IAutoShape shape = slide.getShapes (). AppendShape (ShapeType.CUBE, new Rectangle2D.Double (50,150,150,150); shape.getFill (). SetFillType (FillFormatType.SOLID); shape.getFill (). GetSolidColor (). SetColor (Color.orange); shape.getShapeStyle (). GetLineColor (). SetColor (Color.white); / / animate the shape effect slide.getTimeline (). GetMainSequence (). AddEffect (shape, AnimationEffectType.CHANGE_LINE_COLOR) / / Save the document ppt.saveToFile ("AddAnimationToShape.pptx", FileFormat.PPTX_2013);}}

b. Load the existing PPT document, get the shape animation effect, and set the animation effect. Here you can do more detailed animation settings, including animation repeat type, number of times, duration, delay time and so on.

Import com.spire.presentation.*;import com.spire.presentation.drawing.animation.AnimationEffect;public class RepeatAnimation {public static void main (String [] args) throws Exception {/ / load test document Presentation ppt = new Presentation (); ppt.loadFromFile ("test.pptx"); / / get the first slide ISlide slide = ppt.getSlides (). Get (0); / / get the first animation in the slide AnimationEffect animation = slide.getTimeline (). GetMainSequence (). Get (0) / / set animation loop playback type, number, duration, delay time animation.getTiming () .setAnimationRepeatType (AnimationRepeatType.Number); animation.getTiming () .setRepeatCount (2); / / set repetition times animation.getTiming () .setDuration (2); / / set duration animation.getTiming () .setTriggerDelayTime (2); / / set delay time / / animation.getTiming () .setAnimationRepeatType (AnimationRepeatType.UtilEndOfSlide) / / set the animation loop to the end of the slide / / animation.getTiming (). SetAnimationRepeatType (AnimationRepeatType.UtilNextClick); / / set the animation loop to play until the next click / / save the result document ppt.saveToFile ("RepeatAnimation.pptx", FileFormat.PPTX_2013); ppt.dispose ();}} 2. Add custom animation effects import com.spire.presentation.*;import com.spire.presentation.collections.CommonBehaviorCollection;import com.spire.presentation.drawing.FillFormatType;import com.spire.presentation.drawing.animation.*;import java.awt.*;import java.awt.geom.Point2D;public class CustomAnimationPath {public static void main (String [] args) throws Exception {/ / create a blank PPT document Presentation ppt = new Presentation () / / get the first slide (the newly created slide document already contains a slide by default) ISlide slide = ppt.getSlides () .get (0); / / add shapes to the slide IAutoShape shape = slide.getShapes () .appendShape (ShapeType.FIVE_POINTED_STAR,new Rectangle (180,100,170,170); shape.getFill () .setFillType (FillFormatType.GRADIENT); shape.getFill () .getGradient () .getGradientStops () .append (0, KnownColors.LIGHT_PINK) Shape.getFill (). GetGradient (). GetGradientStops (). Append (1, KnownColors.PURPLE); shape.getShapeStyle (). GetLineColor (). SetColor (Color.white); / / add animation effects and set the animation effect type to PATH_USER (custom type) AnimationEffect effect = slide.getTimeline (). GetMainSequence (). AddEffect (shape, AnimationEffectType.PATH_USER); / / get the CommonBehavior collection of custom animation CommonBehaviorCollection commonBehaviorCollection = effect.getCommonBehaviorCollection () / / set the starting point and path mode of the animated action AnimationMotion motion = (AnimationMotion) commonBehaviorCollection.get (0); motion.setOrigin (AnimationMotionOrigin.LAYOUT); motion.setPathEditMode (AnimationMotionPathEditMode.RELATIVE); / / set the action path MotionPath motionPath = new MotionPath (); motionPath.addPathPoints (MotionCommandPathType.MOVE_TO,new Point2D.Float [] {new Point2D.Float (0L0)}, MotionPathPointsType.CURVE_AUTO,true) MotionPath.addPathPoints (MotionCommandPathType.LINE_TO,new Point2D.Float [] {new Point2D.Float (0.1f) 0.1f)}, MotionPathPointsType.CURVE_AUTO,true); motionPath.addPathPoints (MotionCommandPathType.LINE_TO,new Point2D.Float [] {new Point2D.Float (- 0.1f) 0.2f)}, MotionPathPointsType.CURVE_AUTO,true); motionPath.addPathPoints (MotionCommandPathType.END,new Point2D.Float [] {}, MotionPathPointsType.CURVE_AUTO,true); / / set the action path to the animation motion.setPath (motionPath) / / Save the document ppt.saveToFile ("result.pptx", FileFormat.PPTX_2013); ppt.dispose ();}} so far, I believe you have a deeper understanding of "how to use Java to add animation to PPT". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Animation effects documents slides slides time type shape action number of times path delay loop test content version environment learning different practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft Docker NVidia macOS