<span id="Label3"></p><p><p>We have recently made an educational project. In doing some learning tools, the artist made a number of requirements, roughly the following:</p></p><p><p>In fact, the implementation process is not difficult, the general is to add a circular background to a view control, and then the view is animated, expanded at the same time, the transparency increases, shrinking, while the transparency Decreases.</p></p><p><p>I used the TextView in the example, so first add a rounded background to textview:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><shape xmlns:android= "http://schemas.android.com/apk/res/android" android:shape= "oval" > <size android:height= "90dp" android:width= "90dp"/> < Solid android:color= "@color/color_space_studytools_red"/> <corners android:radius= "180dip"/> <padding android:bottom= "4dip" android:left= "4dip" android:right= "4dip" android:top= "4dip" /></shape></pre></pre><br>Then add animations for textview, including resizing the scaleanimation and adjusting the alphaanimation of the Transparency. The resizing has two parts, expands and shrinks. Contraction immediately after the expansion, that is, the expansion of the animation to monitor, in the Onanimationend () method of contraction,<p><p></p></p><p><p></p></p><pre name="code" class="java">Button simulates heart beating private void Playheartbeatanimation (final View heartbeatview) {animationset swellanimationset = new Animationset (true); swellanimationset.addanimation (new scaleanimation (1.0f, 1.8f, 1.0f, 1.8f, animation.relative_to_ self, 0.5f,animation.relative_to_self, 0.5f)); swellanimationset.addanimation (new alphaanimation (1.0f, 0.3f)); Swellanimationset.setduration (+); swellanimationset.setinterpolator (new accelerateinterpolator ()); Swellanimationset.setfillafter (true); swellanimationset.setanimationlistener (new animationlistener () {@ overridepublic void Onanimationstart (Animation Animation) {} @Overridepublic void Onanimationrepeat (Animation Animation) {} @Overridepublic void onanimationend (animation animation) {animationset shrinkanimationset = new Animationset (true); shrinkanimationset.addanimation (new scaleanimation (1.8f, 1.0f, 1.8f, 1.0f, animation.relative_to _self,0.5f, animation.relative_to_self, 0.5f)); shrinkanimationset.addanimation (new alphaanimation (0.3f, 1.0f)); Shrinkanimationset.sEtduration (+); shrinkanimationset.setinterpolator (new decelerateinterpolator ()); Shrinkanimationset.setfillafter (false); heartbeatview.startanimation (shrinkanimationset);//start again at the end of the animation to achieve a heartbeat view} }); heartbeatview.startanimation (swellanimationset);}</pre><br>The effect of the heartbeat is to keep expanding and shrinking, so open a thread to deal with, whenever the contraction is complete and re-expands.<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Private class Heatbeatthread extends Thread {public void run () {try {sleep ()} catch (interruptedexception e1) {e1.prin Tstacktrace ();} While (true) {runonuithread (new Runnable () {public void run () {for (View view:heartbeatviews) {playheartbeatanimation (vi (ew);}}); Try {thread.sleep;} catch (interruptedexception e) {e.printstacktrace ();}}};}</pre></pre><br>finally, the life cycle of the activity is combined with the start and end of the Thread.<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Private Thread heartbeatthread;/** * start heartbeat */private void startheartbeat () {if (heartbeatthread = = Null) {heartbeatthread = New Heatbeatthread ();} If (!heartbeatthread.isalive ()) {heartbeatthread.start ();}} /** * Stop heartbeat */private void stopheartbeat () {if (heartbeatthread! = null && heartbeatthread.isinterrupted ()) {HEARTB Eatthread.interrupt (); heartbeatthread = null; System.GC ();}} @Overrideprotected void Onresume () {super.onresume (); startheartbeat ();} @Overrideprotected void OnPause () {super.onpause (); stopheartbeat ();}</pre></pre><br><p><p></p></p><p><p>Demo Download As: http://download.csdn.net/detail/u014375869/8838599</p></p><p><p><br></p></p><p><p>Android realizes the Heartbeat effect</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service