讀取動畫變數值
每次應用程式繪製時,都應該讀取動畫變數的目前值,這些值代表要產生動畫效果的視覺特性。
概述
繪製框架時,應用程式可以使用 IUIAnimationVariable::GetValue 或 IUIAnimationVariable::GetIntegerValue 方法來要求任何會影響框架內視覺效果之動畫變數的值。 可以將動畫變數裁剪成一系列值(SetLowerBound 和 SetUpperBound),並使用指定的四捨五入配置要求其值四捨五入為整數(SetRoundingMode)。
而不是讀取每個畫面的所有變數值,應用程式可以使用 IUIAnimationVariable::SetVariableChangeHandler 或 IUIAnimationVariable::SetVariableIntegerChangeHandler 方法來註冊一個或多個變數變更處理程式,並只在變數的值改變時接收通知(IUIAnimationVariableChangeHandler::OnValueChanged),或四捨五入的值(IUIAnimationVariableIntegerChangeHandler::OnIntegerValueChanged)。 若要識別傳遞至變數變更處理程式的變數,應用程式可以使用 IUIAnimationVariable::SetTag 方法,將標記套用至變數。 這些是應用程式所解譯的物件 (IUnknown*), 整陣列。
範例程序代碼
下列範例程式代碼取自 Windows 動畫範例中的 Thumbnail.cpp Grid Layout;請參閱 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 動畫範例中的 MainWindow.cpp,Timer-Driven 動畫;請參閱 CMainWindow::DrawBackground 方法。 它會使用 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
...
}
}
...
}
上一個步驟
開始此步驟之前,您應該已完成此步驟:更新動畫管理員和繪製畫面格。
下一步
完成此步驟之後,下一個步驟是:建立分鏡腳本和新增轉換。
相關主題