How To Make An Animation Grow And Shrink Java
Blitheness resources
An animation resources can define i of two types of animations:
- Property Animation
- Creates an animation past modifying an object's property values over a fix period of time with an
Animator
. - View Animation
-
There are ii types of animations that you tin do with the view blitheness framework:
- Tween animation: Creates an animation by performing a serial of transformations on a single image with an
Animation
- Frame animation: or creates an animation by showing a sequence of images in club with an
AnimationDrawable
.
- Tween animation: Creates an animation by performing a serial of transformations on a single image with an
Property blitheness
An animation divers in XML that modifies backdrop of the target object, such as background color or blastoff value, over a gear up corporeality of time.
- file location:
-
res/animator/filename.xml
The filename will be used as the resource ID. - compiled resource datatype:
- Resource arrow to a
ValueAnimator
,ObjectAnimator
, orAnimatorSet
. - resource reference:
- In Coffee-based or Kotlin code:
R.animator.filename
In XML:@[packet:]animator/filename
- syntax:
-
<fix 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" | "opposite"] android:valueType=["intType" | "floatType"]/> <animator android:duration="int" android:valueFrom="bladder | int | color" android:valueTo="float | int | colour" android:startOffset="int" android:repeatCount="int" android:repeatMode=["restart" | "reverse"] android:valueType=["intType" | "floatType"]/> <set> ... </gear up> </set>
The file must have a single root element: either
<set>
,<objectAnimator>
, or<valueAnimator>
. You can grouping blitheness elements together inside the<ready>
element, including other<gear up>
elements. - elements:
- example:
-
XML file saved at res/animator/property_animator.xml
:<set android:ordering="sequentially"> <set> <objectAnimator android:propertyName="10" android:elapsing="500" android:valueTo="400" android:valueType="intType"/> <objectAnimator android:propertyName="y" android:elapsing="500" android:valueTo="300" android:valueType="intType"/> </set up> <objectAnimator android:propertyName="blastoff" android:elapsing="500" android:valueTo="1f"/> </set>
In guild to run this animation, y'all must inflate the XML resources in your lawmaking to an
AnimatorSet
object, then set the target objects for all of the animations before starting the blitheness 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 set: AnimatorSet = AnimatorInflater.loadAnimator(myContext, R.animator.property_animator) .employ { setTarget(myObject) starting time() }
Coffee
AnimatorSet prepare = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.animator.property_animator); fix.setTarget(myObject); set.showtime();
- see likewise:
-
- Holding Animation
- API Demos for examples on how to employ the property blitheness system.
View animation
The view animation framework supports both tween and frame by frame animations, which can both be alleged in XML. The post-obit sections describe how to use both methods.
Tween animation
An blitheness defined in XML that performs transitions such every bit rotating, fading, moving, and stretching on a graphic.
- file location:
-
res/anim/filename.xml
The filename will be used every bit the resource ID. - compiled resource datatype:
- Resource arrow to an
Animation
. - resource reference:
- In Java:
R.anim.filename
In XML:@[packet:]anim/filename
- syntax:
-
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[packet:]anim/interpolator_resource" android:shareInterpolator=["true" | "false"] > <blastoff android:fromAlpha="float" android:toAlpha="float" /> <scale android:fromXScale="float" android:toXScale="float" android:fromYScale="float" android:toYScale="float" android:pivotX="float" android:pivotY="float" /> <translate android:fromXDelta="float" android:toXDelta="float" android:fromYDelta="bladder" android:toYDelta="float" /> <rotate android:fromDegrees="bladder" android:toDegrees="float" android:pivotX="float" android:pivotY="float" /> <set> ... </set> </set up>
The file must take a unmarried root element: either an
<alpha>
,<calibration>
,<translate>
,<rotate>
, or<set>
element that holds a group (or groups) of other blitheness elements (even nested<gear up>
elements). - elements:
- instance:
-
XML file saved at res/anim/hyperspace_jump.xml
:<ready 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.6" android:pivotX="50%" android:pivotY="fifty%" android:fillAfter="false" android:duration="700" /> <prepare android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="700"> <scale android:fromXScale="one.4" android:toXScale="0.0" android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="fifty%" 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> </set>
This awarding code volition apply the animation to an
ImageView
and start the blitheness:Kotlin
val image: ImageView = findViewById(R.id.image) val hyperspaceJump: Blitheness = AnimationUtils.
loadAnimation
(this, R.anim.hyperspace_jump) image.startAnimation
(hyperspaceJump)Coffee
ImageView image = (ImageView) findViewById(R.id.image); Animation hyperspaceJump = AnimationUtils.
loadAnimation
(this, R.anim.hyperspace_jump); epitome.startAnimation
(hyperspaceJump); - run across also:
-
- 2D Graphics: Tween Animation
Interpolators
An interpolator is an animation modifier defined in XML that affects the rate of change in an blitheness. This allows your existing animation furnishings to be accelerated, decelerated, repeated, bounced, etc.
An interpolator is applied to an animation chemical 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
class. For each interpolator class, Android includes a public resource you lot can reference in club to use the interpolator to an animation using the android:interpolator
attribute. The following tabular array specifies the resource to use for each interpolator:
Interpolator form | Resources 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 |
Here's how you can apply one of these with the android:interpolator
attribute:
<set android:interpolator="@android:anim/accelerate_interpolator"> ... </gear up>
Custom interpolators
If you're not satisfied with the interpolators provided by the platform (listed in the table above), you tin can create a custom interpolator resources with modified attributes. For instance, you tin adjust the charge per unit of acceleration for the AnticipateInterpolator
, or accommodate the number of cycles for the CycleInterpolator
. In order to do so, y'all need to create your own interpolator resource in an XML file.
- file location:
-
res/anim/filename.xml
The filename will be used as the resource ID. - compiled resources datatype:
- Resources pointer 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 yous don't utilise any attributes, then your interpolator will part exactly the same equally those provided by the platform (listed in the table higher up).
- elements:
- Notice that each
Interpolator
implementation, when defined in XML, begins its name in lowercase. - case:
-
XML file saved at
res/anim/my_overshoot_interpolator.xml
:<?xml version="one.0" encoding="utf-eight"?> <overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:tension="seven.0" />
This animation XML will apply the interpolator:
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@anim/my_overshoot_interpolator" android:fromXScale="1.0" android:toXScale="3.0" android:fromYScale="1.0" android:toYScale="3.0" android:pivotX="50%" android:pivotY="50%" android:duration="700" />
Frame blitheness
An blitheness defined in XML that shows a sequence of images in club (like a pic).
- file location:
-
res/drawable/filename.xml
The filename will be used every bit the resource ID. - compiled resource datatype:
- Resources pointer to an
AnimationDrawable
. - resource reference:
- In Coffee:
R.drawable.filename
In XML:@[parcel:]drawable.filename
- syntax:
-
<?xml version="ane.0" encoding="utf-8"?> <animation-listing xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot=["true" | "imitation"] > <particular android:drawable="@[package:]drawable/drawable_resource_name" android:elapsing="integer" /> </animation-list>
- elements:
- example:
-
- XML file saved at
res/drawable/rocket_thrust.xml
: -
<?xml version="1.0" encoding="utf-eight"?> <blitheness-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>
- This application code will fix the animation every bit the background for a View, and so play the animation:
- XML file saved at
- see also:
-
- 2nd Graphics: Frame Animation
Content and code samples on this folio are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2022-01-18 UTC.
Source: https://developer.android.com/guide/topics/resources/animation-resource
Posted by: parkerowle1997.blogspot.com
0 Response to "How To Make An Animation Grow And Shrink Java"
Post a Comment