LoginSignup
0
0

More than 1 year has passed since last update.

JavaでPowerPointにアニメーション効果を追加する方法(プリセットアニメーション/カスタムアニメーション)

Last updated at Posted at 2022-01-11

背景

PowerPointスライドは形状にアニメーション効果を設定することができます。よく見られるアニメーション効果は組み込まれた固定タイプで、つまり、アニメーション効果とパスは事前設定された固定テンプレートですが、アニメーション効果を設計する場合、ユーザーは自分の好みに応じてアニメーションモーションパスを定義してアニメーション効果をカスタマイズすることもできます。以下では、Javaバックエンドプログラムコードを使用して、PowerPointにアニメーション効果を追加する方法を示します。もちろんプリセットアニメーションとアニメーション効果をカスタマイズする方法が含まれています。

テスト環境には次のものが含まれます。

ターゲットテストドキュメント:Power Point 2013
コンパイル環境:IntelliJ IDEA 2018
JDKバージョン:1.8.0
PowerPointライブラリのバージョン:Spire.Presentation.jar 5.1.0

注:このPowerPointライブラリを介してアニメーションタイプ(AnimationEffectType)を追加する場合、約150の異なるタイプを追加できます。

Javaコード一覧

1. プリセットアニメーション効果を追加する

a. 新しいPowerPointドキュメントを作成し、図形を追加し、アニメーション効果を設定する

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);
    }
}

01.png

b. 既存のPowerPointドキュメントをロードし、形状アニメーション効果を取得し、アニメーション効果を設定します。ここでは、アニメーションの繰り返しの種類、回数、継続時間、遅延時間など、より詳細なアニメーション設定を行うことができます。

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("C:\\Users\\Administrator\\Desktop\\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();
    }
}

02.png

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 {
        //空白のPowerPointドキュメントを作成する
        Presentation ppt = new Presentation();

        //最初のスライドを取得する(新しいスライドドキュメントには、デフォルトですでに1つのスライドが含まれている)
        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();
    }
}

03.png

以上は今回のPowerPointにアニメーション効果を追加する方法でした、最後まで読んでいただきありがとうございます、ではまた!

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0