科学风景真好分享 http://blog.sciencenet.cn/u/sunbing01

博文

刘瑞祥:在PPT中用VBA实现路径动画

已有 6701 次阅读 2018-11-20 12:30 |个人分类:编程|系统分类:科普集锦

实例:怎样利用vba制作路径——完全非弹性碰撞动画

  ppt的路径动画无法精确调整路径端点,因此本例用vba解决这一问题。

 我们要做的效果是:左边的球中心运动到(9cm,10cm)时,两球立刻以原来速度的一半向右滑出画面。因此,需要对左边的球(m1)设置两段动画——碰撞前的运动较快,碰撞后的运动较慢,而右边的球(m2)只需要设置一段动画——但要和m1的第二段一起执行。

 2、先把幻灯片页面设为32cm宽,然后进入vba环境,编写下面的程序。

Public Sub AddMotionPath()
    Dim m1 As Shape, m2 As Shape, effNew As Effect, aniMotion As AnimationBehavior

    '绘制两个小球,并分别设置填充色
    Set m1 = ActivePresentation.Slides(1).Shapes.AddShape(Type:=msoShapeOval, _
        Left:=cm2p(1), Top:=cm2p(10), Width:=cm2p(1), Height:=cm2p(1))
    m1.Fill.ForeColor.RGB = RGB(255, 0, 0)
    Set m2 = ActivePresentation.Slides(1).Shapes.AddShape(Type:=msoShapeOval, _
             Left:=cm2p(12), Top:=cm2p(10), Width:=cm2p(1), Height:=cm2p(1))
    m2.Fill.ForeColor.RGB = RGB(0, 0, 255)

    '设置左边小球碰撞前的动画效果
    Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence _
                 AddEffect(Shape:=m1, effectId:=msoAnimEffectPathRight, Trigger:=msoAnimTriggerOnPageClick)

    Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)
    aniMotion.MotionEffect.Path = "M 0 0 L " & 10 / 32 & " 0"
    aniMotion.Timing.Speed = 1

    '设置左边小球碰撞后的动画效果
    Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence _
                 AddEffect(Shape:=m1, effectId:=msoAnimEffectPathRight, Trigger:=msoAnimTriggerAfterPrevious)

    Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)
    aniMotion.MotionEffect.Path = "M " & 10 / 32 & " 0 L 1 0"
    aniMotion.Timing.Speed = 0.5

    '设置右边小球碰撞后的动画效果
    Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence _
                 AddEffect(Shape:=m2, effectId:=msoAnimEffectPathRight, Trigger:=msoAnimTriggerWithPrevious)

    Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)
    aniMotion.MotionEffect.Path = "M 0 0 L " & 22 / 32 & " 0"
    aniMotion.Timing.Speed = 0.5
End Sub

  程序一开始绘制两个小圆形,直径均为1cm,单位需要转化为磅,cm2p()函数见下面。

Private Function cm2p(cm As Single) As Single
    cm2p = cm * 28.35
End Function

  然后,程序添加3段向右运动的效果,AddEffect方法的Shape参数是指定应用动画的对象,effectID指定动画效果,Trigger指定动画发生时间(这里是单击幻灯片页面时发生),后面定义路径(字母M定义起点,M 0 0就是表示对象当前位置为起点,L表示直线运动到的位置,L 10/32 0表示横向向右运动幻灯片宽度的10/32,另外还有C、Z、X、H、V命令),再定义速度。以下定义碰撞以后m1和m2的运动效果,语句大体不变,只改变动画发生时间(m1的为前一动画结束时,m2与m1的第二段动画同时)和速度。

  以上程序只需要执行一遍即设置完毕,然后可以删掉语句。



https://blog.sciencenet.cn/blog-3388899-1147234.html

上一篇:刘瑞祥:在PPT中用VBA实现图形的布尔运算
下一篇:刘瑞祥:怎样利用VBA或者VBS让计算机说话
收藏 IP: 221.197.66.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 23:29

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部