應用程式必須針對使用 Windows 動畫建立動畫的每個視覺特性建立動畫變數。
概述
動畫變數是使用動畫管理員建立的,只要需要,應用程式就應該保留每個變數的參考。 您的應用程式通常會與動畫的視覺物件同時建立每個動畫變數。
建立動畫變數時,必須指定其初始值。 之後,其值只能藉由安排動畫化的分鏡腳本來改變。
建構分鏡腳本時,動畫變數會當做參數傳遞,因此應用程式不應該釋放它們,直到它們所代表的視覺特性不再需要產生動畫效果,通常當相關聯的視覺物件即將終結時。
範例程序代碼
動畫色彩
下列範例程式代碼取自 Windows 動畫範例中的MainWindow.cpp,Application-Driven 動畫 和 Timer-Driven 動畫。 在此範例中,會使用 createAnimationVariable建立三個動畫變數來代表背景色彩。 程序代碼也會使用 SetLowerBound 和 SetUpperBound 方法來控制動畫變數的值。
const DOUBLE INITIAL_RED = COLOR_MAX;
const DOUBLE INITIAL_GREEN = COLOR_MAX;
const DOUBLE INITIAL_BLUE = COLOR_MAX;
HRESULT hr = m_pAnimationManager->CreateAnimationVariable(
INITIAL_RED,
&m_pAnimationVariableRed
);
if (SUCCEEDED(hr))
{
hr = m_pAnimationVariableRed->SetLowerBound(COLOR_MIN);
if (SUCCEEDED(hr))
{
hr = m_pAnimationVariableRed->SetUpperBound(COLOR_MAX);
if (SUCCEEDED(hr))
{
hr = m_pAnimationManager->CreateAnimationVariable(
INITIAL_GREEN,
&m_pAnimationVariableGreen
);
if (SUCCEEDED(hr))
{
hr = m_pAnimationVariableGreen->SetLowerBound(COLOR_MIN);
if (SUCCEEDED(hr))
{
hr = m_pAnimationVariableGreen->SetUpperBound(COLOR_MAX);
if (SUCCEEDED(hr))
{
hr = m_pAnimationManager->CreateAnimationVariable(
INITIAL_BLUE,
&m_pAnimationVariableBlue
);
if (SUCCEEDED(hr))
{
hr = m_pAnimationVariableBlue->SetLowerBound(COLOR_MIN);
if (SUCCEEDED(hr))
{
hr = m_pAnimationVariableBlue->SetUpperBound(COLOR_MAX);
}
}
}
}
}
}
}
}
請注意下列來自 MainWindow.h 的定義。
class CMainWindow
{
...
private:
// Animated Variables
IUIAnimationVariable *m_pAnimationVariableRed;
IUIAnimationVariable *m_pAnimationVariableGreen;
IUIAnimationVariable *m_pAnimationVariableBlue;
...
};
以動畫顯示 x 和 y 座標
下列範例程式代碼取自 Windows 動畫 網格線配置範例中的 Thumbnail.cpp;請參閱 CMainWindow::CreateAnimationVariables 方法。 系統會建立兩個動畫變數來代表每個物件的 X 和 Y 座標。
// Create the animation variables for the x and y coordinates
hr = m_pAnimationManager->CreateAnimationVariable(
xInitial,
&m_pAnimationVariableX
);
if (SUCCEEDED(hr))
{
hr = m_pAnimationManager->CreateAnimationVariable(
yInitial,
&m_pAnimationVariableY
);
...
}
請注意 Thumbnail.h 中的下列定義。
class CThumbnail
{
public:
...
// X and Y Animation Variables
IUIAnimationVariable *m_pAnimationVariableX;
IUIAnimationVariable *m_pAnimationVariableY;
...
};
動畫變數是浮點數,但其值也可以擷取為整數。 根據預設,值會四捨五入為最接近的整數,但可以自訂變數的取整模式。 下列範例程式代碼會使用 SetRoundingMode 方法來指定值一律要四捨五入。
hr = m_pAnimationVariableX->SetRoundingMode(
UI_ANIMATION_ROUNDING_MODE_FLOOR
);
if (SUCCEEDED(hr))
{
hr = m_pAnimationVariableY->SetRoundingMode(
UI_ANIMATION_ROUNDING_MODE_FLOOR
);
...
}
上一個步驟
開始此步驟之前,您應該已完成此步驟:建立主要動畫物件。
下一步
完成此步驟之後,下一個步驟是:更新動畫管理員和繪製畫面格。
相關主題