BackgroundWorker를 사용하여 일부 양식의 불투명도 애니메이션을 만들었습니다.C#에서 BackgroundWorker로 폼 불투명도 애니메이션
이 접근법에는 단지 하나의 작은 문제 만 있지만 문제가 어디인지는 이해할 수 없습니다. 애니메이션 속도는 구성 가능하며 속도 값이 매우 높더라도 애니메이션이 때로는 매우 이상하게 느려지는 경우가 있습니다 ...
"느리게 움직이는 애니메이션"은 말 못하는 것입니다. 애니메이션은 실제로 매우 부드럽습니다. 전체 애니메이션을 수행하는 데 더 많은 시간이 걸립니다 (0 %에서 100 % 또는 부사). 이것은 때때로 발생합니다. 컴퓨터가 다른 다소 집중적 인 배경 작업을 수행 할 때 발생하는 것으로 보입니다 (확실하지 않은 것 같습니다).
물론 수정해야하지만 어쨌든이 코드를 향상 시키거나 다르게 또는 더 잘 수행 할 수 있는지 알고 싶습니다.
private const int TOGGLE_EFFECT_SPEED = 10;
private void blendWorker_DoWork(object sender, DoWorkEventArgs e) {
bool blendIn = (bool)e.Argument;
// Loop through all opacity values
for(double value = 1; value <= 100; value += 1) {
// Report the current progress on the worker
blendWorker.ReportProgress(0, blendIn ? value : 100 - value);
// Suspends the current thread by the specified blend speed
System.Threading.Thread.Sleep(11 - TOGGLE_EFFECT_SPEED);
}
// Set the worker result as the inverse tag value
e.Result = !blendIn;
}
private void blendWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
double opValue = (double)e.UserState;
// Show and repaint the whole main notes window?
if(opValue == 1.0) {
Show();
Invalidate(true);
}
// Set the main notes window opacity value
Opacity = (double)e.UserState/100;
}
private void blendWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
bool tagFlag = (bool)e.Result;
// Hide the main notes window?
if(tagFlag) {
Hide();
}
// Set the main notes window tag value
Tag = tagFlag;
}
/*
THE FOLLOWING METHOD IS PART OF A DIFFERENT CLASS.
ACTUALLY, IT'S THE "PROGRAM" CLASS WHERE MAIN()
IS LOCATED. THIS METHOD IS CALLED TO SHOW/HIDE
THE MAIN APPLICATION FORM WITH AN OPACITY ANIMATION
*/
internal static void ToggleNotesWindow() {
// Get the tag value converted to boolean type
bool tagFlag = Convert.ToBoolean(NotesWindow.Tag, CultureInfo.InvariantCulture);
// Bring the main notes window to front?
if(tagFlag) Program.NotesWindow.BringToFront();
// Run the blend effect if it's not already running
if(!NotesWindow.blendWorker.IsBusy) {
NotesWindow.blendWorker.RunWorkerAsync(tagFlag);
}
// Activate and focus the main notes window?
if(tagFlag) Program.NotesWindow.Activate();
}
나는 그것이 틀린 이유라고 생각하지 않습니다. 나는 확실히 TOGGLE_EFFECT_SPEED = 1 (sleep (10))과 TOGGLE_EFFECT_SPEED = 10 (sleep (1))). 그리고 나는 또한이 두 가지의 차이가 1과 10 사이의 값으로 나타남을 느낍니다. 그리고 속도 값이 중요하지 않기 때문에 "느리게"의미하는 것을 얻지 못했다고 생각합니다. 문제는 속도없이 발생합니다 가치 변화. –
스레드 스케줄러는 대부분의 상황에서 10ms 미만 동안 절전 모드를 유지하지 않으므로 속도 값 1과 10의 차이는 거의 볼 수 없습니다. 따라서 속도는 동일하게 나타납니다. 가장 큰 효과가있는 것은 윈도우와 윈도우를 렌더링하는 데 얼마나 천천히 걸리는지입니다. –