Java 給PPT添加動(dòng)畫效果的示例
PPT幻燈片中對(duì)形狀可設(shè)置動(dòng)畫效果,常見的動(dòng)畫效果為內(nèi)置的固定類型,即動(dòng)畫效果和路徑是預(yù)先設(shè)定好的固定模板,但在設(shè)計(jì)動(dòng)畫效果時(shí),用戶也可以按照自己的喜好自定義動(dòng)畫動(dòng)作路徑。下面,通過Java后端程序代碼來展示如何給PPT添加動(dòng)畫效果。包括預(yù)設(shè)動(dòng)畫以及自定動(dòng)畫效果的方法。
本次測(cè)試環(huán)境包括:
- 目標(biāo)測(cè)試文檔:Power Point 2013
- 編譯環(huán)境:IntelliJ IDEA 2018
- JDK版本:1.8.0
- PPT庫版本:spire.presentation.jar 4.3.2
注:在通過該P(yáng)PT庫來添加動(dòng)畫類型(AnimationEffectType)時(shí),可添加約150種不同類型。
Java程序代碼
1. 添加預(yù)設(shè)動(dòng)畫效果
a. 新建PPT文檔,添加形狀,設(shè)置動(dòng)畫效果
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{
//創(chuàng)建PowerPoint文檔
Presentation ppt = new Presentation();
//獲取幻燈片
ISlide slide = ppt.getSlides().get(0);
//添加一個(gè)形狀到幻燈片
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);
//設(shè)置形狀動(dòng)畫效果
slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.CHANGE_LINE_COLOR);
//保存文檔
ppt.saveToFile("AddAnimationToShape.pptx", FileFormat.PPTX_2013);
}
}

b.加載已有PPT文檔,獲取形狀動(dòng)畫效果,進(jìn)行動(dòng)畫效果設(shè)置,這里可做更為詳細(xì)的動(dòng)畫設(shè)置,包括動(dòng)畫重復(fù)播放類型、次數(shù)、持續(xù)時(shí)間、延遲時(shí)間等.
import com.spire.presentation.*;
import com.spire.presentation.drawing.animation.AnimationEffect;
public class RepeatAnimation {
public static void main(String[] args) throws Exception{
//加載測(cè)試文檔
Presentation ppt = new Presentation();
ppt.loadFromFile("test.pptx");
//獲取第一張幻燈片
ISlide slide = ppt.getSlides().get(0);
//獲取幻燈片中第一個(gè)動(dòng)畫效果
AnimationEffect animation = slide.getTimeline().getMainSequence().get(0);
//設(shè)置動(dòng)畫效果循環(huán)播放類型、次數(shù)、持續(xù)時(shí)間、延遲時(shí)間
animation.getTiming().setAnimationRepeatType(AnimationRepeatType.Number);
animation.getTiming().setRepeatCount(2);//設(shè)置重復(fù)次數(shù)
animation.getTiming().setDuration(2);//設(shè)置持續(xù)時(shí)間
animation.getTiming().setTriggerDelayTime(2);//設(shè)置延遲時(shí)間
//animation.getTiming().setAnimationRepeatType(AnimationRepeatType.UtilEndOfSlide);//設(shè)置動(dòng)畫循環(huán)播放至幻燈片末
//animation.getTiming().setAnimationRepeatType(AnimationRepeatType.UtilNextClick);//設(shè)置動(dòng)畫循環(huán)播放至下次點(diǎn)擊
//保存結(jié)果文檔
ppt.saveToFile("RepeatAnimation.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}

2. 添加自定義動(dòng)畫效果
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 {
//創(chuàng)建一個(gè)空白PPT文檔
Presentation ppt = new Presentation();
//獲取第一張幻燈片(新建的幻燈片文檔默認(rèn)已包含一張幻燈片)
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);
//添加動(dòng)畫效果,并設(shè)置動(dòng)畫效果類型為PATH_USER(自定義類型)
AnimationEffect effect = slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.PATH_USER);
//獲取自定動(dòng)畫的CommonBehavior集合
CommonBehaviorCollection commonBehaviorCollection = effect.getCommonBehaviorCollection();
//設(shè)置動(dòng)畫動(dòng)作運(yùn)動(dòng)起點(diǎn)及路徑模式
AnimationMotion motion = (AnimationMotion)commonBehaviorCollection.get(0);
motion.setOrigin(AnimationMotionOrigin.LAYOUT);
motion.setPathEditMode(AnimationMotionPathEditMode.RELATIVE);
//設(shè)置動(dòng)作路徑
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);
//設(shè)置動(dòng)作路徑到動(dòng)畫
motion.setPath(motionPath);
//保存文檔
ppt.saveToFile("result.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}

以上就是Java 給PPT添加動(dòng)畫效果的示例的詳細(xì)內(nèi)容,更多關(guān)于Java 給PPT添加動(dòng)畫效果的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Java如何為 PPT 中的圖形添加陰影效果
- Java 創(chuàng)建并應(yīng)用PPT幻燈片母版的方法示例
- Java 在PPT中添加文本和圖片超鏈接的實(shí)現(xiàn)方法
- Java 在PPT中添加混合圖表過程詳解
- java實(shí)現(xiàn)在線預(yù)覽--poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
- Java 添加文本框到PPT幻燈片過程解析
- Java如何在PPT中繪制圖形
- 淺談Java設(shè)置PPT幻燈片背景——純色、漸變、圖片背景
- Java使用jacob將微軟office中word、excel、ppt轉(zhuǎn)成pdf
相關(guān)文章
Springboot項(xiàng)目參數(shù)校驗(yàn)方式(Validator)
本文介紹了如何在Spring Boot項(xiàng)目中使用`spring-boot-starter-validation`包和注解來實(shí)現(xiàn)請(qǐng)求參數(shù)校驗(yàn),主要介紹了校驗(yàn)注解的使用方法、校驗(yàn)失敗的異常捕獲以及`@Validated`的分組功能2025-02-02
IDEA設(shè)置maven修改settings.xml配置文件無法加載倉庫的解決方案
這篇文章主要介紹了IDEA設(shè)置maven修改settings.xml配置文件無法加載倉庫的解決方案,幫助大家更好的利用IDEA進(jìn)行JAVA的開發(fā)學(xué)習(xí),感興趣的朋友可以了解下2021-01-01
關(guān)于Rabbitmq死信隊(duì)列及延時(shí)隊(duì)列的實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于Rabbitmq死信隊(duì)列及延時(shí)隊(duì)列的實(shí)現(xiàn),TTL就是消息或者隊(duì)列的過期功能,當(dāng)消息過期就會(huì)進(jìn)到死信隊(duì)列,死信隊(duì)列和普通隊(duì)列沒啥區(qū)別,然后我們只需要配置一個(gè)消費(fèi)者來消費(fèi)死信隊(duì)列里面的消息就可以了,需要的朋友可以參考下2023-08-08
Java編程實(shí)現(xiàn)用hash方法切割文件
這篇文章主要介紹了Java編程實(shí)現(xiàn)用hash方法切割文件,簡(jiǎn)單介紹了hash的概念,然后分享了使用方法示例,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12
解決IDEA 左側(cè)Project中沒有out文件夾的問題
這篇文章主要介紹了解決IDEA 左側(cè)Project中沒有out文件夾的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java使用elasticsearch基礎(chǔ)API使用案例講解
這篇文章主要介紹了Java使用elasticsearch基礎(chǔ)API使用案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

