在 Android 簡報中套用形狀動畫

概述

Aspose.Slides for Android via Java 將投影片動畫表示為投影片時間軸中的效果。每個效果都有目標形狀、動畫類型與子類型、觸發方式、時間設定,以及可選的屬性,例如聲音或動畫結束後的行為。

時間軸包含兩種序列:

  • 主序列 在投影片前進時播放。
  • 互動序列 在其觸發形狀被點擊時開始。

因為文字方塊、圖片、圖表、表格和其他投影片物件皆實作IShape,您可以對大多數投影片內容使用相同的ISequence.addEffect方法。可用的效果列在EffectType類別中。

新增形狀動畫

要新增動畫,取得投影片的主序列,然後使用目標形狀、效果類型、子類型與觸發方式呼叫ISequence.addEffect。若想建立在另一個形狀被點擊時才開始的效果,請建立觸發該形狀的互動序列。

以下範例同時建立兩種動畫,並將結果儲存為shape-animations.pptx

import com.aspose.slides.*;

public class AddShapeAnimations {
    public static void main(String[] args) {
        Presentation presentation = new Presentation();
        try {
            ISlide slide = presentation.getSlides().get_Item(0);

            IAutoShape targetShape = slide.getShapes().addAutoShape(ShapeType.RoundCornerRectangle, 120, 100, 320, 80);
            targetShape.addTextFrame("Click to animate this shape");

            ISequence mainSequence = slide.getTimeline().getMainSequence();
            IEffect entranceEffect = mainSequence.addEffect(targetShape, EffectType.Fade, EffectSubtype.None, EffectTriggerType.OnClick);
            entranceEffect.getTiming().setDuration(1.5f);

            IAutoShape triggerShape = slide.getShapes().addAutoShape(ShapeType.Bevel, 20, 20, 100, 40);
            triggerShape.addTextFrame("Move");

            ISequence interactiveSequence = slide.getTimeline().getInteractiveSequences().add(triggerShape);
            interactiveSequence.addEffect(targetShape, EffectType.PathFootball, EffectSubtype.None, EffectTriggerType.OnClick);

            presentation.save("shape-animations.pptx", SaveFormat.Pptx);
        } finally {
            presentation.dispose();
        }
    }
}

觸發器決定何時開始效果:

若要為圖片、圖表或其他形狀類型加入動畫,請將該物件傳遞給ISequence.addEffect,而非targetShape。有關圖表專屬的分組選項,請參閱Animated Charts

讀取形狀動畫

當您已知目標形狀時,使用ISequence.getEffectsByShape。若要檢視每個效果,請列舉主序列和所有互動序列。列舉可避免假設序列在索引0處一定有效果。

以下範例建立具有主序列與互動效果的形狀,取得針對該形狀的效果,然後列舉投影片上的每個序列。

import com.aspose.slides.*;

public class ReadShapeAnimations {
    public static void main(String[] args) {
        Presentation presentation = new Presentation();
        try {
            ISlide slide = presentation.getSlides().get_Item(0);
            IAutoShape targetShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 120, 100, 320, 80);
            targetShape.addTextFrame("Animated shape");

            ISequence mainSequence = slide.getTimeline().getMainSequence();
            mainSequence.addEffect(targetShape, EffectType.Fade, EffectSubtype.None, EffectTriggerType.OnClick);

            IAutoShape triggerShape = slide.getShapes().addAutoShape(ShapeType.Bevel, 20, 20, 100, 40);
            triggerShape.addTextFrame("Move");

            ISequence interactiveSequence = slide.getTimeline().getInteractiveSequences().add(triggerShape);
            interactiveSequence.addEffect(targetShape, EffectType.PathFootball, EffectSubtype.None, EffectTriggerType.OnClick);

            IEffect[] targetEffects = mainSequence.getEffectsByShape(targetShape);
            System.out.println("The main sequence contains " + targetEffects.length + " effect(s) for " + targetShape.getName() + ".");

            printSequence("Main sequence", mainSequence);

            int interactiveIndex = 1;
            for (ISequence sequence : slide.getTimeline().getInteractiveSequences()) {
                String triggerName = sequence.getTriggerShape() == null ? "unknown" : sequence.getTriggerShape().getName();
                String sequenceLabel = "Interactive sequence " + interactiveIndex + ", trigger: " + triggerName;
                printSequence(sequenceLabel, sequence);
                interactiveIndex++;
            }
        } finally {
            presentation.dispose();
        }
    }

    private static void printSequence(String label, ISequence sequence) {
        System.out.println("  " + label + ": " + sequence.getCount() + " effect(s)");

        for (IEffect effect : sequence) {
            String targetName = effect.getTargetShape() == null ? "unknown" : effect.getTargetShape().getName();
            String typeName = EffectType.getName(EffectType.class, effect.getType());
            String subtypeName = EffectSubtype.getName(EffectSubtype.class, effect.getSubtype());
            String triggerName = EffectTriggerType.getName(EffectTriggerType.class, effect.getTiming().getTriggerType());
            String effectDescription = typeName + " " + subtypeName + "; target: " + targetName + "; trigger: " + triggerName;
            System.out.println("    " + effectDescription);
        }
    }
}

如果僅需要單一形狀的效果,請先以名稱、佔位元類型或其他穩定屬性識別該形狀;然後呼叫ISequence.getEffectsByShape。不要假設索引0IShapeCollection.get_Item必定是目標物件。

處理繼承佔位元的效果

一般投影片上的佔位元可以繼承自版面投影片與母片投影片中對應佔位元的動畫行為。IShape.getBasePlaceholder 會回傳該父佔位元,若不存在則回傳null

在下列範例簡報中,頁腳在一般投影片上使用Random Bars,在版面投影片上使用Split,在母片上使用Fly In

一般投影片上頁腳的動畫效果

版面投影片上頁腳的佔位元動畫效果

母片投影片上頁腳的佔位元動畫效果

接下來的範例使用新簡報中的佔位元層級。它為母片佔位元、版面佔位元以及一般投影片上的相應佔位元加入效果。每次呼叫IShape.getBasePlaceholder 前皆先檢查回傳的形狀是否為null

import com.aspose.slides.*;

public class InheritedPlaceholderAnimations {
    public static void main(String[] args) {
        Presentation presentation = new Presentation();
        try {
            ILayoutSlide layoutSlide = presentation.getLayoutSlides().getByType(SlideLayoutType.TitleAndObject);
            IShape layoutPlaceholder = findPlaceholderWithBase(layoutSlide);

            if (layoutPlaceholder == null) {
                throw new IllegalStateException("The layout slide does not contain a placeholder linked to its master slide.");
            }

            IShape masterPlaceholder = layoutPlaceholder.getBasePlaceholder();
            layoutSlide.getMasterSlide().getTimeline().getMainSequence().addEffect(masterPlaceholder, EffectType.Fly, EffectSubtype.Bottom, EffectTriggerType.OnClick);
            layoutSlide.getTimeline().getMainSequence().addEffect(layoutPlaceholder, EffectType.Split, EffectSubtype.VerticalIn, EffectTriggerType.OnClick);

            ISlide slide = presentation.getSlides().addEmptySlide(layoutSlide);
            IShape slidePlaceholder = findPlaceholderWithBase(slide, layoutPlaceholder);

            if (slidePlaceholder == null) {
                throw new IllegalStateException("The slide does not contain a placeholder linked to its layout slide.");
            }

            slide.getTimeline().getMainSequence().addEffect(slidePlaceholder, EffectType.RandomBars, EffectSubtype.Horizontal, EffectTriggerType.OnClick);
            printEffects("Normal slide", slide.getTimeline().getMainSequence().getEffectsByShape(slidePlaceholder));

            IShape baseLayoutPlaceholder = slidePlaceholder.getBasePlaceholder();
            if (baseLayoutPlaceholder != null) {
                printEffects("Layout slide", layoutSlide.getTimeline().getMainSequence().getEffectsByShape(baseLayoutPlaceholder));

                IShape baseMasterPlaceholder = baseLayoutPlaceholder.getBasePlaceholder();
                if (baseMasterPlaceholder != null) {
                    printEffects("Master slide", layoutSlide.getMasterSlide().getTimeline().getMainSequence().getEffectsByShape(baseMasterPlaceholder));
                }
            }

            presentation.save("placeholder-animations.pptx", SaveFormat.Pptx);
        } finally {
            presentation.dispose();
        }
    }

    private static IShape findPlaceholderWithBase(ILayoutSlide layoutSlide) {
        for (IShape shape : layoutSlide.getShapes()) {
            if (shape.getBasePlaceholder() != null) {
                return shape;
            }
        }

        return null;
    }

    private static IShape findPlaceholderWithBase(ISlide slide, IShape expectedBase) {
        for (IShape shape : slide.getShapes()) {
            if (shape.getBasePlaceholder() == expectedBase) {
                return shape;
            }
        }

        return null;
    }

    private static void printEffects(String source, IEffect[] effects) {
        System.out.println(source + ": " + effects.length + " effect(s)");

        for (IEffect effect : effects) {
            String typeName = EffectType.getName(EffectType.class, effect.getType());
            String subtypeName = EffectSubtype.getName(EffectSubtype.class, effect.getSubtype());
            System.out.println("  " + typeName + " " + subtypeName);
        }
    }
}

變更動畫時間設定

PowerPoint的Timing對話方塊對應到ITiming的屬性。

PowerPoint動畫效果的Timing對話方塊

此獨立範例加入一個效果,透過ISequence.addEffect取得的物件變更其時間設定,並儲存結果。保留回傳的IEffect參考,可避免不必要的集合索引。

import com.aspose.slides.*;

public class ChangeAnimationTiming {
    public static void main(String[] args) {
        Presentation presentation = new Presentation();
        try {
            ISlide slide = presentation.getSlides().get_Item(0);
            IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 120, 100, 320, 80);
            shape.addTextFrame("Timed animation");

            IEffect effect = slide.getTimeline().getMainSequence().addEffect(shape, EffectType.Fade, EffectSubtype.None, EffectTriggerType.OnClick);
            effect.getTiming().setTriggerType(EffectTriggerType.OnClick);
            effect.getTiming().setDuration(2.0f);
            effect.getTiming().setTriggerDelayTime(0.5f);
            effect.getTiming().setRepeatUntilNextClick(false);
            effect.getTiming().setRepeatUntilEndSlide(false);
            effect.getTiming().setRepeatCount(2.0f);
            effect.getTiming().setRewind(true);

            presentation.save("shape-animation-timing.pptx", SaveFormat.Pptx);
        } finally {
            presentation.dispose();
        }
    }
}

請只使用一種重複模式。將重複次數與「直到」旗標同時使用可能在不同的檢視器中產生混亂的結果。變更重複模式時,請先設定ITiming.setRepeatUntilNextClickITiming.setRepeatUntilEndSlide,再設定ITiming.setRepeatCount,因為設定任一旗標同時會改變目前的重複模式。

新增與擷取動畫聲音

動畫效果可以透過IEffect.getSound參考嵌入的音訊。IEffect.setStopPreviousSound 可讓效果停止先前效果所啟動的音訊。

將聲音加入效果

以下範例假設本機有名為animation-sound.wav的音訊檔案。它建立兩個效果,將該檔案嵌入為第一個效果的聲音,並設定第二個效果停止該聲音。它使用由ISequence.addEffect回傳的物件,因此不需要序列索引。

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class AddAnimationSound {
    public static void main(String[] args) throws IOException {
        Presentation presentation = new Presentation();
        try {
            ISlide slide = presentation.getSlides().get_Item(0);
            IAutoShape firstShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 80, 100, 240, 80);
            IAutoShape secondShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 400, 100, 240, 80);
            firstShape.addTextFrame("Starts sound");
            secondShape.addTextFrame("Stops sound");

            ISequence sequence = slide.getTimeline().getMainSequence();
            IEffect firstEffect = sequence.addEffect(firstShape, EffectType.Fade, EffectSubtype.None, EffectTriggerType.OnClick);
            IEffect secondEffect = sequence.addEffect(secondShape, EffectType.Fade, EffectSubtype.None, EffectTriggerType.OnClick);

            byte[] audioData = Files.readAllBytes(Paths.get("animation-sound.wav"));
            IAudio effectSound = presentation.getAudios().addAudio(audioData);
            firstEffect.setSound(effectSound);
            secondEffect.setStopPreviousSound(true);

            presentation.save("shape-animation-sound.pptx", SaveFormat.Pptx);
        } finally {
            presentation.dispose();
        }
    }
}

擷取嵌入的效果聲音

以下範例假設本機有名為presentation-with-animation-sounds.pptx的簡報。它同時掃描主序列與互動序列,並將每個嵌入的效果聲音寫入extracted-animation-sounds目錄。副檔名取自IAudio.getContentType所回傳的音訊MIME類型。

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;

public class ExtractAnimationSounds {
    public static void main(String[] args) throws IOException {
        Path inputPath = Paths.get("presentation-with-animation-sounds.pptx");
        Path outputDirectory = Paths.get("extracted-animation-sounds");

        Files.createDirectories(outputDirectory);

        Presentation presentation = new Presentation(inputPath.toString());
        try {
            int soundIndex = 1;

            for (ISlide slide : presentation.getSlides()) {
                soundIndex = saveSounds(slide.getTimeline().getMainSequence(), outputDirectory, soundIndex);

                for (ISequence sequence : slide.getTimeline().getInteractiveSequences()) {
                    soundIndex = saveSounds(sequence, outputDirectory, soundIndex);
                }
            }

            System.out.println("Extracted " + (soundIndex - 1) + " sound file(s) to " + outputDirectory.toAbsolutePath() + ".");
        } finally {
            presentation.dispose();
        }
    }

    private static int saveSounds(ISequence sequence, Path outputDirectory, int soundIndex) throws IOException {
        for (IEffect effect : sequence) {
            if (effect.getSound() == null) {
                continue;
            }

            String extension = getAudioExtension(effect.getSound().getContentType());
            Path outputPath = outputDirectory.resolve("effect-sound-" + soundIndex + extension);
            Files.write(outputPath, effect.getSound().getBinaryData());
            soundIndex++;
        }

        return soundIndex;
    }

    private static String getAudioExtension(String contentType) {
        String normalizedType = contentType == null ? "" : contentType.toLowerCase(Locale.ROOT);

        if (normalizedType.equals("audio/mpeg")) {
            return ".mp3";
        }

        if (normalizedType.equals("audio/mp4")) {
            return ".m4a";
        }

        if (normalizedType.equals("audio/ogg")) {
            return ".ogg";
        }

        if (normalizedType.equals("audio/wav") || normalizedType.equals("audio/x-wav")) {
            return ".wav";
        }

        return ".bin";
    }
}

對於大型音訊物件,請使用IAudio.getStream並將串流複製至檔案,而不是將整個物件載入至位元組陣列。

設定動畫結束後的行為

After animation選項決定形狀在效果結束後的處理方式。

PowerPoint效果選項對話方塊顯示After animation設定

AfterAnimationType類別支援保持形狀不變、變更其顏色、動畫結束後隱藏,或在下一次點擊時隱藏。當類型為AfterAnimationType.Color時,同時設定IEffect.getAfterAnimationColor

此獨立範例建立一個效果,透過回傳的效果物件設定其動畫結束後的行為,並儲存結果。

import com.aspose.slides.*;
import android.graphics.Color;

public class SetAfterAnimationBehavior {
    public static void main(String[] args) {
        Presentation presentation = new Presentation();
        try {
            ISlide slide = presentation.getSlides().get_Item(0);
            IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 120, 100, 320, 80);
            shape.addTextFrame("Dim after animation");

            IEffect effect = slide.getTimeline().getMainSequence().addEffect(shape, EffectType.Fade, EffectSubtype.None, EffectTriggerType.OnClick);
            effect.setAfterAnimationType(AfterAnimationType.Color);
            effect.getAfterAnimationColor().setColor(Color.LTGRAY);

            presentation.save("shape-animation-after-effect.pptx", SaveFormat.Pptx);
        } finally {
            presentation.dispose();
        }
    }
}

將類型從AfterAnimationType.Color改變會清除動畫結束後的顏色設定。

文字動畫

文字動畫有兩個相關控制項:

以下獨立範例為文字方塊中的詞句加入動畫。BuildType.AsOneObject 會停用逐段落建立,讓字詞設定套用於整個文字框。

import com.aspose.slides.*;

public class AnimateTextByWord {
    public static void main(String[] args) {
        Presentation presentation = new Presentation();
        try {
            ISlide slide = presentation.getSlides().get_Item(0);
            IAutoShape textBox = slide.getShapes().addAutoShape(ShapeType.Rectangle, 80, 80, 560, 100);
            textBox.addTextFrame("Aspose.Slides animates this sentence word by word.");

            IEffect effect = slide.getTimeline().getMainSequence().addEffect(textBox, EffectType.Fade, EffectSubtype.None, EffectTriggerType.OnClick);
            effect.getTextAnimation().setBuildType(BuildType.AsOneObject);
            effect.setAnimateTextType(AnimateTextType.ByWord);
            effect.setDelayBetweenTextParts(20.0f);

            presentation.save("animated-text.pptx", SaveFormat.Pptx);
        } finally {
            presentation.dispose();
        }
    }
}

若要逐段落建立文字方塊,請設定BuildType.ByLevelParagraphs1(或其他段落層級)。若要為單一段落指定其自身效果,請使用接受IParagraphISequence.addEffect之多載。請參閱Animated Text取得段落層級範例。

匯出與相容性說明

  • 儲存為 PPT 或 PPTX 會保留動畫模式,但最終播放由簡報檢視器控制。
  • PDF 與靜態圖像不會播放動畫。當需要顯示動作時,請使用HTML5 export、動畫 GIF 或video conversion
  • 若匯出為 HTML5,請啟用Html5Options.setAnimateShapes,且在需要時啟用Html5Options.setAnimateTransitions
  • 影片轉換支援許多常見的進入、強調、退出與路徑動畫效果,但並非所有 PowerPoint 效果皆受支援。請檢查目前的supported animations and effects並使用目標 Aspose.Slides 版本測試關鍵簡報。
  • 進階自訂效果與從其他簡報格式匯入的效果可能會在檔案中保留,但在 PowerPoint、HTML5 或影片中呈現方式不同。請驗證匯出結果,而非僅依賴效果名稱。

常見問題

為什麼動畫在 PowerPoint 中顯示,但在 PDF 中卻沒有?

PDF 為靜態格式,故不會播放動畫與投影片過場。若必須保留動作,請匯出為 HTML5、動畫 GIF 或影片。

為什麼效果在影片中播放方式不同?

影片匯出會將動畫渲染成影片,而非儲存原始的 PowerPoint 行為。某些進階效果不受支援或會被近似。請檢查受支援的效果表,並在正式使用前測試實際簡報。

將形狀前移或後移會改變其動畫順序嗎?

不會。形狀的 Z 順序僅控制重疊,動畫的播放順序由序列順序與觸發方式決定。如需不同的播放順序,請變更時間軸。