Android animation basics
In android there are 4 basic types of animations:
- AlphaAnimation
- RotateAnimation
- ScaleAnimation
- TranslateAnimation
This animations can be highly customized and combined, for example we can set the speed, delay, acceleration, duration of animations, then group them together in an AnimationSet.
Animations can be applyed as Layout animations to ViewGroups, to be triggered when the ViewGroup is created/displayed or applied to any view and be triggered by us any time.
The animations can be definex in XMl or by code. Defining in xml is mutch more clear, but you can not set animation parameters dinamically.
The animation XML files should be created in the res/anim folder and can be accessed using the R class, like R.anim.animation_name.
An animation defined in hyperspace_jump.xml can look like this:
- <?xml version="1.0" encoding="UTF-8"?>
- <set xmlns:android="http://schemas.android.c
om/apk/res/android"> - <scale android:interpolator="@android:anim/accelerate
_decelerate_interpolator" - android:fromXScale="0.0" android:toXScale="1.4" android:fromYScale="0.0"
- android:toYScale="1.4" android:pivotX="50%" android:pivotY="50%"
- android:fillAfter="false" android:duration="700" />
- <scale android:fromXScale="1.4" android:toXScale="0.8"
- android:fromYScale="1.4" android:toYScale="0.8" android:pivotX="50%"
- android:pivotY="50%" android:startOffset="700" android:duration="400"
- android:fillBefore="false" />
- </set>
The set tag can contain one or more animation definitions that will be applied at the same time, so if you want to play them after each other you can set a startOffset value for the second one.
This animation can be played on any view with a code like this:
The exapmle below shows the creation of an animation without an XML file:
- //parameters set to do a full circle around its center
- RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- rotateAnimation.setDuration(500);
- rotateAnimation.setRepeatCount(2);
- //you can set many other parameters here
- button1.startAnimation(rotateAnimation);
Creating complex animation sets are mutch more clean in XML, the example below creates an animation set from 3 animations by code:
- AnimationSet animationSet = new AnimationSet(true);
- RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- rotateAnimation.setDuration(500);
- rotateAnimation.setRepeatCount(2);
- animationSet.addAnimation(rotateAnimation);
- TranslateAnimation translateAnimation = new TranslateAnimation(0, 400, 0, 0);
- //setting offset and duration to start after first rotation completed, and end at the same time with the last roration
- translateAnimation.setStartOffset(500);
- translateAnimation.setDuration(1000);
- animationSet.addAnimation(translateAnimation);
- AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
- alphaAnimation.setStartOffset(500);
- alphaAnimation.setDuration(1000);
- animationSet.addAnimation(alphaAnimation);
- button2.startAnimation(animationSet);
If we want do apply the animation already defined in hyperspace_jump.xml as a layout animation, to play on all element in the group we need one more XML file in the res/anim folder to define this animation as a layout animation. Lets name this file as main_layout_animation.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <layoutAnimation xmlns:android="http://schemas.android.c
om/apk/res/android" - android:animation="@anim/hyperspace_jump&qu
ot; />
After this we can apply this animation to a viewGrop, for example to a LinearLayout:
- LinearLayout mainContainer = (LinearLayout) findViewById(R.id.mainContainer);
- LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.main_layout_animation);
- mainContainer.setLayoutAnimation(controller);
You can download and try out a working example, with the code fragments above here.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
12 years 3 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
12 years 3 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
12 years 3 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
12 years 3 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
12 years 3 weeks ago
Poll
Useful resources
Android Development Projects
- App Development
- Telegram bulk messages sender
- Photo Editing Application
- React Native Developer - part time basis
- Locket App Copy
- Courier app $150 only
- Mobile Application Development - IOS and Android (Rewards for sharing brand ads on social media)
- Android app for sms
- Fitness app project for sale online
- io developer