Friday, January 18, 2019

How to animation in XML on Android

Android offers to developers a great Animation API that lets them to create easily frame animation in XML or programmatically. In the fol... thumbnail 1 summary
Android offers to developers a great Animation API that lets them to create easily frame animation in XML or programmatically. In the following tutorial, you’re going to learn how to create a frame animation in XML. For this tutorial, we’re going to use a monster character that we want to animate.
Step 1. Create all animation of characte









 

Step 2. Create animation in XML

 
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/monster0" android:duration="250"/> <item android:drawable="@drawable/monster1" android:duration="250"/> <item android:drawable="@drawable/monster2" android:duration="250"/> <item android:drawable="@drawable/monster3" android:duration="250"/> <item android:drawable="@drawable/monster4" android:duration="250"/> <item android:drawable="@drawable/monster5" android:duration="250"/> <item android:drawable="@drawable/monster6" android:duration="250"/> <item android:drawable="@drawable/monster7" android:duration="250"/> <item android:drawable="@drawable/monster8" android:duration="250"/> <item android:drawable="@drawable/monster9" android:duration="250"/> </animation-list> Step 3. Create the layout activity_main.xml
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#FFFFFF" tools:context="com.ssaurel.animationsprite.MainActivity" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/run_monster" /> </RelativeLayout> Step 4. Run animation on class Main_activity.class
public class MainActivity extends Activity { private ImageView img; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView) findViewById(R.id.img); img.post(new Runnable() { @Override public void run() { ((AnimationDrawable) img.getBackground()).start(); } }); } }

No comments

Post a Comment