Java 给PPT添加动画效果(预设动画/自定义动画)
PPT幻灯片中对形状可设置动画效果,常见的动画效果为内置的固定类型,即动画效果和路径是预先设定好的固定模板,但在设计动画效果时,用户也可以按照自己的喜好自定义动画动作路径。下面,通过Java后端程序代码来展示如何给PPT添加动画效果。包括预设动画以及自定动画效果的方法。
本次测试环境包括:
- 目标测试文档:Power Point 2013
- 编译环境:IntelliJ IDEA 2018
- JDK版本:1.8.0
- PPT库版本:spire.presentation.jar 4.3.2
注:在通过该PPT库来添加动画类型(AnimationEffectType)时,可添加约150种不同类型。
Java程序代码
1. 添加预设动画效果
a. 新建PPT文档,添加形状,设置动画效果
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{ //创建PowerPoint文档 Presentation ppt = new Presentation(); //获取幻灯片 ISlide slide = ppt.getSlides().get(0); //添加一个形状到幻灯片 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); //设置形状动画效果 slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.CHANGE_LINE_COLOR); //保存文档 ppt.saveToFile("AddAnimationToShape.pptx", FileFormat.PPTX_2013); } }
b.加载已有PPT文档,获取形状动画效果,进行动画效果设置,这里可做更为详细的动画设置,包括动画重复播放类型、次数、持续时间、延迟时间等.
import com.spire.presentation.*; import com.spire.presentation.drawing.animation.AnimationEffect; public class RepeatAnimation { public static void main(String[] args) throws Exception{ //加载测试文档 Presentation ppt = new Presentation(); ppt.loadFromFile("test.pptx"); //获取第一张幻灯片 ISlide slide = ppt.getSlides().get(0); //获取幻灯片中第一个动画效果 AnimationEffect animation = slide.getTimeline().getMainSequence().get(0); //设置动画效果循环播放类型、次数、持续时间、延迟时间 animation.getTiming().setAnimationRepeatType(AnimationRepeatType.Number); animation.getTiming().setRepeatCount(2);//设置重复次数 animation.getTiming().setDuration(2);//设置持续时间 animation.getTiming().setTriggerDelayTime(2);//设置延迟时间 //animation.getTiming().setAnimationRepeatType(AnimationRepeatType.UtilEndOfSlide);//设置动画循环播放至幻灯片末 //animation.getTiming().setAnimationRepeatType(AnimationRepeatType.UtilNextClick);//设置动画循环播放至下次点击 //保存结果文档 ppt.saveToFile("RepeatAnimation.pptx", FileFormat.PPTX_2013); ppt.dispose(); } }
2. 添加自定义动画效果
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 { //创建一个空白PPT文档 Presentation ppt = new Presentation(); //获取第一张幻灯片(新建的幻灯片文档默认已包含一张幻灯片) ISlide slide = ppt.getSlides().get(0); //添加形状到幻灯片 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); //添加动画效果,并设置动画效果类型为PATH_USER(自定义类型) AnimationEffect effect = slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.PATH_USER); //获取自定动画的CommonBehavior集合 CommonBehaviorCollection commonBehaviorCollection = effect.getCommonBehaviorCollection(); //设置动画动作运动起点及路径模式 AnimationMotion motion = (AnimationMotion)commonBehaviorCollection.get(0); motion.setOrigin(AnimationMotionOrigin.LAYOUT); motion.setPathEditMode(AnimationMotionPathEditMode.RELATIVE); //设置动作路径 MotionPath motionPath = new MotionPath(); motionPath.addPathPoints(MotionCommandPathType.MOVE_TO,new Point2D.Float[]{new Point2D.Float(0,0)},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); //设置动作路径到动画 motion.setPath(motionPath); //保存文档 ppt.saveToFile("result.pptx", FileFormat.PPTX_2013); ppt.dispose(); } }
以上是本次Java给PPT添加动画效果的全部内容。如需转载,请注明出处!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2016-03-30 在Excel表里面插入背景图