Which Statement Should Be Placed In An Animation-list To Make An Animation Play Once And Then Stop?
Blitheness resources
An animation resource can define one of two types of animations:
- Property Animation
- Creates an blitheness by modifying an object'south property values over a set menses of time with an
Animator
. - View Blitheness
-
There are two types of animations that you can practise with the view blitheness framework:
- Tween animation: Creates an animation by performing a series of transformations on a single image with an
Animation
- Frame animation: or creates an blitheness past showing a sequence of images in social club with an
AnimationDrawable
.
- Tween animation: Creates an animation by performing a series of transformations on a single image with an
Property blitheness
An animation divers in XML that modifies properties of the target object, such every bit background color or alpha value, over a set amount of fourth dimension.
- file location:
-
res/animator/filename.xml
The filename will be used as the resources ID. - compiled resource datatype:
- Resources arrow to a
ValueAnimator
,ObjectAnimator
, orAnimatorSet
. - resource reference:
- In Coffee-based or Kotlin code:
R.animator.filename
In XML:@[packet:]animator/filename
- syntax:
-
<set android:ordering=["together" | "sequentially"]> <objectAnimator android:propertyName="string" android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["restart" | "contrary"] android:valueType=["intType" | "floatType"]/> <animator android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["restart" | "reverse"] android:valueType=["intType" | "floatType"]/> <set> ... </set> </gear up>
The file must have a single root chemical element: either
<set up>
,<objectAnimator>
, or<valueAnimator>
. You tin can group animation elements together inside the<set up>
element, including other<prepare>
elements. - elements:
- example:
-
XML file saved at res/animator/property_animator.xml
:<set android:ordering="sequentially"> <gear up> <objectAnimator android:propertyName="ten" android:elapsing="500" android:valueTo="400" android:valueType="intType"/> <objectAnimator android:propertyName="y" android:duration="500" android:valueTo="300" android:valueType="intType"/> </set> <objectAnimator android:propertyName="alpha" android:elapsing="500" android:valueTo="1f"/> </set up>
In order to run this animation, you must inflate the XML resources in your code to an
AnimatorSet
object, and so fix the target objects for all of the animations earlier starting the animation set. CallingsetTarget()
sets a single target object for all children of theAnimatorSet
equally a convenience. The following code shows how to do this:Kotlin
val fix: AnimatorSet = AnimatorInflater.loadAnimator(myContext, R.animator.property_animator) .apply { setTarget(myObject) start() }
Java
AnimatorSet fix = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.animator.property_animator); set.setTarget(myObject); set up.start();
- see as well:
-
- Property Animation
- API Demos for examples on how to utilise the holding blitheness system.
View blitheness
The view blitheness framework supports both tween and frame by frame animations, which can both exist declared in XML. The post-obit sections describe how to use both methods.
Tween animation
An animation defined in XML that performs transitions such as rotating, fading, moving, and stretching on a graphic.
- file location:
-
res/anim/filename.xml
The filename will be used as the resource ID. - compiled resources datatype:
- Resource arrow to an
Blitheness
. - resource reference:
- In Java:
R.anim.filename
In XML:@[package:]anim/filename
- syntax:
-
<?xml version="ane.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[package:]anim/interpolator_resource" android:shareInterpolator=["true" | "false"] > <alpha android:fromAlpha="bladder" android:toAlpha="float" /> <scale android:fromXScale="bladder" android:toXScale="float" android:fromYScale="float" android:toYScale="bladder" android:pivotX="float" android:pivotY="float" /> <translate android:fromXDelta="float" android:toXDelta="float" android:fromYDelta="bladder" android:toYDelta="bladder" /> <rotate android:fromDegrees="bladder" android:toDegrees="float" android:pivotX="bladder" android:pivotY="float" /> <fix> ... </set> </gear up>
The file must have a single root element: either an
<alpha>
,<scale>
,<translate>
,<rotate>
, or<set>
chemical element that holds a group (or groups) of other animation elements (fifty-fifty nested<set>
elements). - elements:
- example:
-
XML file saved at res/anim/hyperspace_jump.xml
:<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.4" android:fromYScale="1.0" android:toYScale="0.six" android:pivotX="50%" android:pivotY="50%" android:fillAfter="fake" android:duration="700" /> <set android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="700"> <calibration android:fromXScale="1.4" android:toXScale="0.0" android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:elapsing="400" /> <rotate android:fromDegrees="0" android:toDegrees="-45" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="400" /> </set> </prepare>
This application code will apply the animation to an
ImageView
and offset the animation:Kotlin
val epitome: ImageView = findViewById(R.id.image) val hyperspaceJump: Animation = AnimationUtils.
loadAnimation
(this, R.anim.hyperspace_jump) image.startAnimation
(hyperspaceJump)Java
ImageView epitome = (ImageView) findViewById(R.id.image); Animation hyperspaceJump = AnimationUtils.
loadAnimation
(this, R.anim.hyperspace_jump); image.startAnimation
(hyperspaceJump); - see also:
-
- 2D Graphics: Tween Animation
Interpolators
An interpolator is an animation modifier defined in XML that affects the charge per unit of change in an animation. This allows your existing animation furnishings to be accelerated, decelerated, repeated, bounced, etc.
An interpolator is applied to an blitheness element with the android:interpolator
attribute, the value of which is a reference to an interpolator resource.
All interpolators bachelor in Android are subclasses of the Interpolator
course. For each interpolator course, Android includes a public resource yous tin can reference in society to apply the interpolator to an animation using the android:interpolator
aspect. The following table specifies the resource to use for each interpolator:
Interpolator class | Resource ID |
---|---|
AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator |
AccelerateInterpolator | @android:anim/accelerate_interpolator |
AnticipateInterpolator | @android:anim/anticipate_interpolator |
AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator |
BounceInterpolator | @android:anim/bounce_interpolator |
CycleInterpolator | @android:anim/cycle_interpolator |
DecelerateInterpolator | @android:anim/decelerate_interpolator |
LinearInterpolator | @android:anim/linear_interpolator |
OvershootInterpolator | @android:anim/overshoot_interpolator |
Hither's how you can apply ane of these with the android:interpolator
attribute:
<set android:interpolator="@android:anim/accelerate_interpolator"> ... </prepare>
Custom interpolators
If yous're not satisfied with the interpolators provided by the platform (listed in the table above), you can create a custom interpolator resource with modified attributes. For example, you tin can adjust the rate of acceleration for the AnticipateInterpolator
, or suit the number of cycles for the CycleInterpolator
. In order to do so, you need to create your own interpolator resource in an XML file.
- file location:
-
res/anim/filename.xml
The filename volition be used equally the resource ID. - compiled resource datatype:
- Resource arrow to the corresponding interpolator object.
- resources reference:
- In XML:
@[package:]anim/filename
- syntax:
-
<?xml version="1.0" encoding="utf-8"?> <InterpolatorName xmlns:android="http://schemas.android.com/apk/res/android" android:attribute_name="value" />
If you don't apply any attributes, then your interpolator will function exactly the aforementioned every bit those provided past the platform (listed in the table in a higher place).
- elements:
- Notice that each
Interpolator
implementation, when defined in XML, begins its name in lowercase. - instance:
-
XML file saved at
res/anim/my_overshoot_interpolator.xml
:<?xml version="1.0" encoding="utf-eight"?> <overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:tension="vii.0" />
This blitheness XML will employ the interpolator:
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@anim/my_overshoot_interpolator" android:fromXScale="1.0" android:toXScale="iii.0" android:fromYScale="1.0" android:toYScale="3.0" android:pivotX="50%" android:pivotY="50%" android:elapsing="700" />
Frame blitheness
An animation defined in XML that shows a sequence of images in society (similar a film).
- file location:
-
res/drawable/filename.xml
The filename will be used as the resource ID. - compiled resources datatype:
- Resources pointer to an
AnimationDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML:@[packet:]drawable.filename
- syntax:
-
<?xml version="ane.0" encoding="utf-viii"?> <blitheness-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot=["truthful" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource_name" android:duration="integer" /> </animation-list>
- elements:
- instance:
-
- XML file saved at
res/drawable/rocket_thrust.xml
: -
<?xml version="i.0" encoding="utf-viii"?> <blitheness-listing xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <particular android:drawable="@drawable/rocket_thrust1" android:duration="200" /> <particular android:drawable="@drawable/rocket_thrust2" android:elapsing="200" /> <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>
- This application code will set the animation equally the background for a View, then play the blitheness:
- XML file saved at
- meet also:
-
- 2D Graphics: Frame Animation
Content and code samples on this page are subject field to the licenses described in the Content License. Coffee and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Final updated 2022-01-18 UTC.
Source: https://developer.android.com/guide/topics/resources/animation-resource
Posted by: hansoneachich.blogspot.com
0 Response to "Which Statement Should Be Placed In An Animation-list To Make An Animation Play Once And Then Stop?"
Post a Comment