Code of sample android application of Frame-by-Frame Animation using a sequence of three images.
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dharmik.p9"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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" tools:context="com.example.dharmik.p9.MainActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="66dp" android:id="@+id/iv_1" tools:ignore="ContentDescription" /> <Button android:text="@string/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="76dp" android:id="@+id/bt_1" tools:ignore="RelativeOverlap" android:layout_marginLeft="36dp" android:layout_marginStart="36dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:text="@string/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt_2" android:layout_marginRight="33dp" android:layout_marginEnd="33dp" android:layout_alignBaseline="@+id/bt_1" android:layout_alignBottom="@+id/bt_1" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" tools:ignore="RelativeOverlap" /> </RelativeLayout>
//strings.xml
<resources>
<string name="app_name">P9</string>
<string name="start">start</string>
<string name="stop">stop</string>
</resources>
//animation.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/an1"
android:duration="200"/>
<item
android:drawable="@drawable/an2"
android:duration="200"/>
<item
android:drawable="@drawable/an3"
android:duration="200"/>
<item
android:drawable="@drawable/an4"
android:duration="200"/>
<item
android:drawable="@drawable/an5"
android:duration="100"/>
<item
android:drawable="@drawable/an6"
android:duration="100"/>
<item
android:drawable="@drawable/an7"
android:duration="100"/>
<item
android:drawable="@drawable/an8"
android:duration="100"/>
<item
android:drawable="@drawable/an9"
android:duration="100"/>
<item
android:drawable="@drawable/an10"
android:duration="100"/>
<item
android:drawable="@drawable/an11"
android:duration="100"/>
<item
android:drawable="@drawable/an12"
android:duration="100"/>
<item
android:drawable="@drawable/an13"
android:duration="100"/>
<item
android:drawable="@drawable/an14"
android:duration="100"/>
</animation-list>
//MainActivity.java
package com.example.dharmik.p9;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.provider.Settings;
import android.support.v4.graphics.BitmapCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button start,stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button)findViewById(R.id.bt_1);
stop=(Button)findViewById(R.id.bt_2);
final ImageView myAnView=(ImageView)findViewById(R.id.iv_1);
myAnView.setBackgroundResource(R.drawable.animation);
final AnimationDrawable myAnimation=(AnimationDrawable)myAnView.getBackground();
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAnView.post(new Runnable() {
@Override
public void run() {
myAnimation.start();
}
});
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAnimation.stop();
System.exit(0);
}
});
}
}
OUTPUT:-
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment