瀵逛簬Swing紼嬪簭鏉ヨjava.swing.Timer綾諱繚璇佷簡(jiǎn)綰跨▼鍦╯wing璋冪敤涓婄殑瀹夊叏鎬с傞氳繃鏃墮棿鍙傛暟鐨勮緗椂闂村姩鎬佸畾鏃跺埛鏂幫紝
瀵逛簬鍔ㄦ佸線澶嶆弿緇樻潵璇達(dá)紝姣斿綾諱技浜庡姩鎬佺殑棰滆壊鍙樺寲錛屽姩鎬佺殑榪涜閫忔槑鍙樺寲涔嬬被鐨勫懆鏈熸у埛鏂版潵璇達(dá)紝涓鑸渶瑕佸嚑涓潯浠?br />
1.鍔ㄧ敾鐨勫懆鏈熸?br />
2.鍔ㄧ敾鐨勫綋鍓嶇姸鎬佸湪璧峰鐘舵佸拰鐩爣鐘舵佷箣闂?br />
瀹炵幇涓婇渶瑕佽繖涔堝嚑涓弬鏁?br />
- 璧峰鏃墮棿 animation startTime
- 褰撳墠鏃墮棿 currentime
- 鍔ㄧ敾鍛ㄦ湡 animation duration
- 寰榪斿洜鏁?fraction
寰榪斿洜鏁癴raction
姣斿鍔ㄦ佽皟鏁撮忔槑搴︺佸姩鎬佷慨鏀歸鑹插湪鍔ㄧ敾鐨勮繃紼嬩腑鍙互璁懼畾璧峰涓庣洰鏍囧鹼紝閫氳繃fraction鍦?-1鑼冨洿鍐呰繘琛岃繍綆楄繘琛岃皟鏁淬?br />
浠ョ畻娉曟潵鎻忚堪鍒欎負(fù)
璧峰鍊艱涓?init
鐩爣鍊間負(fù) dest
瀹為檯鍊間負(fù) actual
actual=init*(1-fraction)+dest*fraction;
姣旇緝鏄庢樉鐨勪緥瀛愪負(fù)錛屽皢棰滆壊浠庡垵濮嬮鑹插姩鎬佸彉鍖栧埌鐩爣棰滆壊
Color startColor = Color.red; // where we start
Color endColor = Color.BLACK; // where we end
Color currentColor = startColor;
....
鎻忕粯currentColor鐨勪竴涓渾
鍦═imer鐨刟ctionPerform閲岃皟鏁碿urrentColor
// interpolate between start and end colors with current fraction
int red = (int)(fraction * endColor.getRed() +
(1 - fraction) * startColor.getRed());
int green = (int)(fraction * endColor.getGreen() +
(1 - fraction) * startColor.getGreen());
int blue = (int)(fraction * endColor.getBlue() +
(1 - fraction) * startColor.getBlue());
// set our new color appropriately
currentColor = new Color(red, green, blue);
閫氳繃瀹氭椂鍣ㄧ殑鏃墮棿鍙傛暟鍔ㄦ佽皟鏁村線榪斿洜鏁?/h2>
閫氳繃鏃墮棿鍙傛暟榪涜璁$畻
濡備笅浠g爜鎵紺猴紝鍦═imer鐨刟ctionPerform閲屽疄鐜?br />
long currentTime = System.nanoTime() / 1000000;
long totalTime = currentTime - animationStartTime;
//璋冩暣鍛ㄦ湡鐨勮搗濮嬫椂闂?br />
if (totalTime > animationDuration) {
animationStartTime = currentTime;
}
float fraction = (float)totalTime / animationDuration;
fraction = Math.min(1.0f, fraction);
娉ㄦ剰褰撳墠鍙槸璁$畻鍑轟簡(jiǎn)fraction錛屽浣曚嬌鍥犲瓙鍦?-0鍜?-1涔嬮棿寰澶嶅彉鍖栧憿
浠ヤ笅浠g爜瀹炵幇浜?jiǎn)璇ソ帡娉?br />
// This calculation will cause alpha to go from 1 to 0 and back to 1
// as the fraction goes from 0 to 1
alpha = Math.abs(1 - (2 * fraction));
//repaint();//閲嶆柊緇樺埗

]]>