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添加動畫效果的全部内容。如需轉載,請注明出處!