讀取動畫變數值

每次應用程式繪製時,它都應該讀取動畫變數的目前值,這些值代表要產生動畫效果的視覺特性。

概觀

繪製框架時,應用程式可以使用 IUIAnimationVariable::GetValueIUIAnimationVariable::GetIntegerValue 方法來要求任何會影響畫面內視覺效果的動畫變數值。 您可以 (SetLowerBoundSetUpperBound) 將動畫變數裁剪成某個範圍的值,並使用指定的進位配置 (SetRoundingMode) ,要求其值四捨五入為整數。

而不是讀取每個畫面的所有變數值。 應用程式可以使用 IUIAnimationVariable::SetVariableChangeHandlerIUIAnimationVariable::SetVariableIntegerChangeHandler 方法來註冊一或多個變數變更處理常式,只有在變數的值變更 (IUIAnimationVariableChangeHandler::OnValueChanged) 或四捨五入值時,才會收到通知 (IUIAnimationVariableIntegerChangeHandler::OnIntegerValueChanged) 。 若要識別傳遞給變數變更處理常式的變數,應用程式可以使用 IUIAnimationVariable::SetTag 方法將標記套用至變數。 這些是物件 (IUnknown*) ,應用程式所解譯的整陣列。

範例程式碼

下列範例程式碼取自 Windows 動畫範例 方格配置中的 Thumbnail.cpp;請參閱 CMainWindow::Render 方法。 它會使用 GetValue 方法,將值讀取為浮點值。

// Get the x-coordinate and y-coordinate animation variable values

DOUBLE x=0;
hr = m_pAnimationVariableX->GetValue(&x);
if (SUCCEEDED(hr))
{
    DOUBLE y=0;
    hr = m_pAnimationVariableY->GetValue(&y);
    if (SUCCEEDED(hr))
    {
        // Draw the object

        ...

    }
}

下列範例程式碼取自 Windows 動畫範例 Timer-Driven Animation中的 MainWindow.cpp;請參閱 CMainWindow::D rawBackground 方法。 它會使用 GetIntegerValue 方法,將值讀取為整數值。

// Get the RGB animation variable values

INT32 red;
HRESULT hr = m_pAnimationVariableRed->GetIntegerValue(
    &red
    );
if (SUCCEEDED(hr))
{
    INT32 green;
    hr = m_pAnimationVariableGreen->GetIntegerValue(
        &green
        );
    if (SUCCEEDED(hr))
    {
        INT32 blue;
        hr = m_pAnimationVariableBlue->GetIntegerValue(
            &blue
            );
        if (SUCCEEDED(hr))
        {
            // Set the RGB of the background brush to the new animated value

            ...
                
            // Paint the background

            ...

        }
    }

    ...

}

上一個步驟

開始此步驟之前,您應該已完成此步驟: 更新動畫管理員和繪製畫面格

後續步驟

完成此步驟之後,下一個步驟是: 建立分鏡腳本和新增轉換

IUIAnimationVariable::GetIntegerValue

IUIAnimationVariable::GetValue

Windows 動畫概觀