Fade in-fade out effect problem
Below is a snippet of code from my custom button laf. The thing is that i want alpha to take values from range [0.5, 1.0], or some other values from range [0.0, 1.0]. I have managed to do that with the code below for a range [0.5, 1.0], but focusAlpha repeats itself for a while with the minimum value of 0.5 from a range [0.5, 1.0]. I know that is because focusAlpha = Math.abs(1.0f - (2*fraction)); where focusAlpha goes from 1.0 to 0.0 and back. But how to make alpha to take values from 1.0 to 0.5 and back without repeating the minimum value in between?
<br />
Timer FocusButtonTimer = new Timer(30, new ActionListener() {</p>
<p> public void actionPerformed(ActionEvent e) {</p>
<p> long currentTime = System.nanoTime() / 1000000;<br />
long totalTime = currentTime - animationStartTime;<br />
if (totalTime > animationDurationFocus) {<br />
animationStartTime = currentTime;<br />
}<br />
float fraction = (float) totalTime / animationDurationFocus;<br />
fraction = Math.min(1.0f, fraction);</p>
<p> focusAlpha = Math.abs(1.0f - (2*fraction));</p>
<p> focusAlpha = Math.min(1.0f, focusAlpha);<br />
focusAlpha = Math.max(0.5f, focusAlpha);</p>
<p> c.repaint();<br />
}<br />
});<br />




I've solved it.
</p> <pre class="bb-code-block"> Timer FocusButtonTimer = new Timer(30, new ActionListener() { public void actionPerformed(ActionEvent e) { long currentTime = System.nanoTime() / 1000000; long totalTime = currentTime - animationStartTime; if (totalTime > animationDurationFocus) { animationStartTime = currentTime; } float fraction = (float) totalTime / animationDurationFocus; fraction = Math.min(1.0f, fraction); focusAlpha = Math.abs(1.0f - fraction); if (focusAlpha < 0.5f) { focusAlpha = 1-focusAlpha; } c.repaint(); } });