本文介绍如何使用spire.presentation for java在ppt幻灯片中设置自定义动画路径。
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();
}
}
设置结果: