Alpha. xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:startOffset="200" android:duration="1000"/></set>
Rotate. xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:fromDegrees="0" android:toDegrees="-360" android:pivotX="50%p" android:pivotY="50%" android:duration="1000"/></set>
Scale. xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1.0" android:toXScale="0" android:fromYScale="1.0" android:toYScale="0" android:pivotX="50%" android:pivotY="50%" android:duration="1000"/></set>
Translate. xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="50%" android:toXDelta="100%" android:fromYDelta="50%" android:toYDelta="200%" android:duration="2000"/></set>
Mainactivity. Java
Package COM. kale. anim; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. animation. alphaanimation; import android. view. animation. animation; import android. view. animation. animationset; import android. view. animation. animationutils; import android. view. animation. rotateanimation; import android. view. animation. scaleanimation; import android. view. animation. translateanimation; import android. widget. imageview; public class mainactivity extends activity {imageview IV; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); IV = (imageview) findviewbyid (R. id. imageview);} public void buttonlistener (view v) {animation anim = NULL; Switch (v. GETID () {case R. id. alpha_button: anim = animationutils. loadanimation (getapplicationcontext (), R. anim. alpha); break; case R. id. rotate_button: anim = animationutils. loadanimation (getapplicationcontext (), R. anim. rotate); break; case R. id. scale_button: anim = animationutils. loadanimation (getapplicationcontext (), R. anim. scale); break; case R. id. translate_button: anim = animationutils. loadanimation (getapplicationcontext (), R. anim. translate); break; default: break;} // sets the animation to start iv. startanimation (anim );}}
You can use the tool class to load XML files so that you can use XML files. Finally, paste the XML interface file.
<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 = "$ {relativepackage }. $ {activityclass} "> <linearlayout Android: Id =" @ + ID/linearlayout1 "Android: layout_width =" match_parent "Android: layout_height =" wrap_content "Android: layout_alignparenttop = "true" Android: layout_centerhorizontal = "true" Android: layout_margintop = "28dp" Android: Orientation = "vertical"> <button Android: id = "@ + ID/alpha_button" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: onclick = "buttonlistener" Android: TEXT = "Transparency changes"/> <button Android: Id = "@ + ID/rotate_button" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: onclick = "buttonlistener" Android: text = "Rotating Animation"/> <button Android: Id = "@ + ID/scale_button" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: onclick = "buttonlistener" Android: text = "Scaling Animation"/> <button Android: Id = "@ + ID/translate_button" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: onclick = "buttonlistener" Android: text = "Mobile Animation"/> </linearlayout> <imageview Android: id = "@ + ID/imageview" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_below = "@ + ID/linearlayout1" Android: layout_centerhorizontal = "true" Android: layout_margintop = "56dp" Android: src = "@ drawable/ic_launcher"/> </relativelayout>
About animation rate settings.
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ Android: anim/accelerate_interpolator"
Android: Required interpolator = "true">
......
</Set>
Example:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:startOffset="200" android:duration="1000"/></set>
You can set it in the Code as follows:
AnimationSet set = new AnimationSet(true); set.setInterpolator(new DecelerateInterpolator());
Use XML files to set animations