首页 > 知识问答文章正文

Java 自定义PPT动画动作路径

知识问答 2022-02-13 15:09:28 admin

PPT幻灯片中对形状可设置动画效果,常见的动画效果为内置的固定类型,即动画效果和路径是预先设定好的固定模板,但在设计动画效果时,用户也可以按照自己的喜好自定义动画动作路径。下面,通过Java后端程序代码来展示如何来实现自定义动作路径。

工具/材料

目标测试文档:Power Point 2013

编译环境:IntelliJ IDEA 2018

JDK版本:1.8.0

PPT库:spire.presentation.jar 4.3.2

操作方法

【步骤01】

关于文档路径:本次测试环境中为方便文档管理,将文档放在IDEA项目文件夹下(这里的文件路径可以另外自定义)。如下图:

【步骤02】

在IDEA程序中引入spire.presentation.jar工具,如下图

【步骤03】

在程序中键入如下代码内容:
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();
}
}

【步骤04】

完成代码后,执行程序,生成文档(如上图1中,文档路径),打开文档后,可查看自定义动画路径效果:

标签:

发表评论

评论列表

可盼网Copyright @ 2011-2024 All Rights Reserved. |版权所有 备案号: 豫ICP备2022006457号-8