Android tween animation cannot be stopped

Source: Internet
Author: User

Today, when I wrote a heartbeat animation, I found it could not be stopped.

1: The animation anim. xml file is as follows:</P> <p> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <set Android: preset interpolator = "false" xmlns: Android = "http://schemas.android.com/apk/res/android"> </P> <p> <scale Android: Duration = "335" Android: fillafter = "true" Android: fromxscale = "1.0" Android: fromyscale = "1.0" Android: interpolator = "@ Android: anim/linear_interpolator" Android: Export Tx = "50%" Android: required ty = "50%" Android: repeatcount = "infinite" Android: startoffset = "700" Android: toxscale = "1.4" Andro ID: toyscale = "1.4" type = "codeph" text = "codeph"> <br/> <! -- </P> <p> fromxscale [float] <br/> fromyscale [float] indicates the scaling size on the X and Y coordinates when the animation starts; 0.0 indicates the scale to none; 1.0 indicates normal scaling. A value smaller than 1.0 indicates contraction. if the value is greater than 1.0, toxscale [float] is enlarged. <br/> toyscale [float] indicates that the animation ends, the scaling size of X and Y coordinates, TX [float] <br/> ty [float], indicates the attribute value of the starting position of the animation relative to the X and Y coordinates of the object: from 0% to 100%, 50% indicates the point position on the X or Y coordinate of the object <br/> --> </P> <p> </scale> </set> <br/>2: The layout code of activity_main.xml is as follows:<Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" tools: context = ". mainactivity "> </P> <p> <imageview Android: Id =" @ + ID/imageview "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: layout_centerhorizontal = "true" Android: layout_centervertical = "true" Android: Background = "@ anim/frame_anim" Android: visibility = "invisible"> </P> <p> <button Android: Id = "@ + ID/start" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "start"> </P> <p> </button> <button Android: Id = "@ + ID/end" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparenttop = "true" Android: layout_torightof = "@ + ID/start" Android: TEXT = "end"> <br/> </button> </imageview> </relativelayout> </P> <p>3: the Java code is as follows:</P> <p> package COM. youpeng. animationdemo; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. view. animation. animation; <br/> Import android. widget. button; <br/> Import android. widget. imageview; </P> <p> public class mainactivity extends activity implements onclicklistener {<br/> private imageview mimageview; <br/> private button start, end; <br/> private animation manimation </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. activity_main); </P> <p> mimageview = (imageview) findviewbyid (R. id. imageview); </P> <p> Start = (button) findviewbyid (R. id. start); <br/> Start. setonclicklistener (this); <br/> end = (button) findviewbyid (R. id. end); <br/> end. setonclicklistener (this); <br/> manimation = animationutils. loadanimation (this, R. anim. anim); </P> <p> // mimageview. setbackgroundresource (R. anim. frame_anim); <br/> // manimationdrawable = (animationdrawable) mimageview. getbackground (); </P> <p >}< br/> // <br/> @ override <br/> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> switch (v. GETID () {<br/> case R. id. start: <br/> mimageview. startanimation (manimation); </P> <p> mimageview. setvisibility (view. visible); <br/> log. E ("START", "Start"); <br/> break; <br/> case R. id. end: <br/> // mimageview. setvisibility (view. gone); <br/> mimageview. clearanimation (); <br/> log. E ("end", "end"); </P> <p> break; <br/> default: <br/> break; <br/>}< br/>4: There are two methods in the animation class: Start () and startnow (). Both methods can start the animation, but to stop the animation, there is a canel (), however, it seems that the animation cannot be stopped all the time, so there are two methods in the View class: startanimation () and clearanimation (). These two methods can start and stop the animation, but you want to make the animation message on the Interface
Add a setvisibility (view. Gone) for this component ).

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.