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添加动画效果的全部内容。如需转载,请注明出处!

Java 给PPT添加动画效果(预设动画/自定义动画)的更多相关文章

  1. Android 动画之View动画效果和Activity切换动画效果

    View动画效果: 1.>>Tween动画通过对View的内容进行一系列的图形变换(平移.缩放.旋转.透明度变换)实现动画效果,补间动画需要使用<set>节点作为根节点,子节点 ...

  2. css动画效果之transition(动画过渡效果属性)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. AndroidUI 视图动画-混合动画效果 (AnimationSet)/动画效果监听

    在前面介绍了几种动画效果:透明动画效果(AlphsAnimation).移动动画效果(TranslateAnimation).旋转动画效果(RotateAnimation).缩放动画效果(ScaleA ...

  4. CSS3动画效果——js调用css动画属性并回调处理详解

    http://www.jb51.net/css/258407.html 这篇文章主要详细介绍了CSS3动画效果回调处理,需要的朋友可以参考下 我们在做js动画的时候,很多时候都需要做回调处理,如在一个 ...

  5. css3动画效果:3 3D动画

    立方体旋转动画效果 css #container{ width: 400px; height: 400px; ; ; -webkit-perspective-origin:50% 225px; per ...

  6. css动画效果之transition(动画效果属性)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. css3动画效果:2 简易动画

    1. transition动画:鼠标移上去  旋转放大 关键点--  :hover  \ transform: scale(*)  rotate(*deg) cards 2.关键帧动画: 位移动画 t ...

  8. Android动画效果之自定义ViewGroup添加布局动画

    前言: 前面几篇文章介绍了补间动画.逐帧动画.属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画.本文将通 ...

  9. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

随机推荐

  1. WebIDE All In One

    WebIDE All In One web IDE Visual Studio Code vscode Code editing Redefined. Free. Built on open sour ...

  2. GraphQL API In Action

    GraphQL API In Action GraphQL API express $ yarn add express express-graphql graphql # OR $ npm i -S ...

  3. whiteboard & coding interview practice

    whiteboard & coding interview practice 白板 & 面试 & 编码练习 Algorithm https://www.freecodecamp ...

  4. Arctic Code Vault Contributor

    Arctic Code Vault Contributor GitHub Archive Program https://archiveprogram.github.com/ Preserving o ...

  5. ASCII Art

    ASCII Art https://npms.io/search?q=ASCII art ASCII Art Text to ASCII Art Generator (TAAG) http://pat ...

  6. how to input special keyboard symbol in macOS(⌘⇧⌃⌥)

    how to input special keyboard symbol in macOS(⌘⇧⌃⌥) emoji ctrl + command + space / ⌘⇧⌃ ⌘⇧⌃ Character ...

  7. CSS & SASS & SCSS & less

    CSS & SASS & SCSS & less less vs scss https://github.com/vecerek/less2sass/wiki/Less-vs. ...

  8. Typescript & React & Vue

    Typescript & React & Vue Typescript & React https://facebook.github.io/create-react-app/ ...

  9. 「NGK每日快讯」11.25日NGK公链第23期官方快讯!

  10. 21_MySQL表外连接实战

    -- 查询每名员工的编号.姓名.部门.月薪.工资等级.工龄.上司编号.上司姓名.上司部门? SELECT e.empno,#员工编号 e.ename,#员工姓名 e.deptno,#员工部门 e.sa ...