Skip to content

AnimationDemo.lua

xu.jingyu edited this page Dec 24, 2019 · 4 revisions

AnimationDemo.lua

copy下面代码,运行试试

-- 示例:TransX
local baseView =  Label():text("TransX"):textColor(Color(0,0,0,1))
baseView:y(100):x(0):width(50):height(50)
baseView:bgColor(Color(0, 225, 225, 1))
baseView:fontSize(15)
window:addView(baseView)
isAnimating = false
animation = Animation()
animation:setStartCallback(function()
    print("|| Animation start!")
end)
animation:setEndCallback(function()
    print("|| Animation end!")
end)
baseView:onClick(function()
    animation:setTranslateX(0, 100)
    animation:setRotate(0, 180)
    animation:setDuration(3)
    animation:setRepeat(RepeatType.REVERSE, 2)
    animation:setInterpolator(InterpolatorType.AccelerateDecelerate)
    animation:start(baseView)
    isAnimating = true
end)

-- 动画组合
local baseView1 =  Label():text("组合"):textColor(Color(0,0,0,1))
baseView1:y(260):x(150):width(80):height(50)
baseView1:bgColor(Color(0, 225, 225, 1))
window:addView(baseView1)
animation1 = Animation()
animation1:setStartCallback(function()
    print("|| Animation 1 start!")
end)
animation1:setEndCallback(function()
    print("|| Animation 1 end!")
end)
baseView1:onClick(function()
    animation1:setScaleX(0.5, 1.2)
    animation1:setScaleY(0.5, 1.2)
    animation1:setTranslateX(150, 150)
    animation1:setTranslateY(260, 260)
    animation1:setAlpha(1.0, 0.1)
    animation1:setDuration(3)
    animation1:setDelay(3)
    animation1:setAutoBack(true)
    animation1:setRepeat(RepeatType.REVERSE, 2)
    animation1:start(baseView1)
end)

-- 示例:TransY
baseView2 =  Label():text("TransY"):textColor(Color(0,0,0,1))
baseView2:y(180):x(0):width(50):height(50)
baseView2:bgColor(Color(0, 225, 225, 1))
baseView2:fontSize(15)
window:addView(baseView2)
isAnimating = false
animation2 = Animation()
animation2:setStartCallback(function()
    print("|| Animation2 start!")
end)
animation2:setEndCallback(function()
    print("|| Animation2 end!")
end)
baseView2:onClick(function()
    animation2:setTranslateY(0, 100)
    animation2:setRotate(0, 180)
    animation2:setDuration(3)
    animation2:setInterpolator(InterpolatorType.Bounce)
    animation2:start(baseView2)
    isAnimating = true
end)

-- 示例:ScaleX
baseView3 =  Label():text("ScaleX"):textColor(Color(0,0,0,1))
baseView3:y(260):x(50):width(80):height(50)
baseView3:bgColor(Color(0, 225, 225, 1))
window:addView(baseView3)
animation3 = Animation()
animation3:setStartCallback(function()
    print("|| Animation3 start!")
end)
animation3:setEndCallback(function()
    print("|| Animation3 end!")
end)
baseView3:onClick(function()
    animation3:setScaleX(0.5, 1.2)
    animation3:setDuration(3)
    animation3:setInterpolator(InterpolatorType.Bounce)
    animation3:start(baseView3)
end)

-- 示例:ScaleY
baseView4 = Label():text("ScaleY"):textColor(Color(0,0,0,1))
baseView4:y(340):x(80):width(80):height(50)
baseView4:bgColor(Color(0, 225, 225, 1))
window:addView(baseView4)
animation4 = Animation()
animation4:setStartCallback(function()
    print("|| Animation start!")
end)
animation4:setEndCallback(function()
    print("|| Animation end!")
end)
baseView4:onClick(function()
    animation4:setScaleY(0.5, 5)
    animation4:setDuration(3)
    animation4:setInterpolator(InterpolatorType.Overshoot)
    animation4:setInterpolator(InterpolatorType.Bounce)
    animation4:start(baseView4)
end)

-- 示例:Alpha
baseView5 = Label():text("Alpha"):textColor(Color(0,0,0,1))
baseView5:y(420):x(0):width(50):height(50)
baseView5:bgColor(Color(88, 125, 56, 1))
window:addView(baseView5)
animation5 = Animation()
animation5:setStartCallback(function()
    print("|| Animation start!")
end)
animation5:setEndCallback(function()
    print("|| Animation end!")
end)
baseView5:onClick(function()
    animation5:setAlpha(1.0, 0.1)
    animation5:setDuration(3)
    animation5:setAutoBack(false)
    animation5:setInterpolator(InterpolatorType.Bounce)
    animation5:start(baseView5)
end)

-- 示例:delay
baseView6 = Label():text("delay"):textColor(Color(0,0,0,1))
baseView6:y(500):x(0):width(50):height(50)
baseView6:bgColor(Color(88, 125, 56, 1))
window:addView(baseView6)
animation6 = Animation()
animation6:setStartCallback(function()
    print("|| Animation6 start!")
end)
animation6:setEndCallback(function()
    print("|| Animation6 end!")
end)
baseView6:onClick(function()
    animation6:setAlpha(1.0, 0.1)
    animation6:setDuration(3)
    animation6:setDelay(2)
    animation6:setInterpolator(InterpolatorType.Bounce)
    animation6:start(baseView6)
end)


window:onClick(function()
    if isAnimating then
        animation:pause()
        isAnimating = false
    else
        animation:resume()
        isAnimating = true
    end
end)
Clone this wiki locally