HOW TO IMPLEMENTS FRAME BY FRAME ANIMATION
Framelayout.xmlFile
frame.xmlFile
<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=".MainActivity"
>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="400dp"
android:text="start" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/stop"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp"
/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/start"
android:layout_alignBottom="@+id/start"
android:layout_alignParentRight="true"
android:layout_marginRight="26dp"
android:text="Stop" />
</RelativeLayout>
frame.java File
package com.example.framebyframeanim;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageView imv;
AnimationDrawable anim;
Button start,stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imv=(ImageView)findViewById(R.id.imageView1);
anim=new AnimationDrawable();
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
imv.setBackgroundResource(R.drawable.aa);
BitmapDrawable f1=(BitmapDrawable)getResources().getDrawable(R.drawable.aa);
BitmapDrawable f2=(BitmapDrawable)getResources().getDrawable(R.drawable.bb);
BitmapDrawable f3=(BitmapDrawable)getResources().getDrawable(R.drawable.cc);
BitmapDrawable f4=(BitmapDrawable)getResources().getDrawable(R.drawable.dd);
BitmapDrawable f5=(BitmapDrawable)getResources().getDrawable(R.drawable.xx);
anim.addFrame(f1, 200);
anim.addFrame(f2, 200);
anim.addFrame(f3, 200);
anim.addFrame(f4, 200);
anim.addFrame(f5, 200);
anim.setOneShot(false);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), "aaaaa", Toast.LENGTH_LONG).show();
imv.setBackgroundDrawable(anim);
anim.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imv.setBackgroundDrawable(anim);
anim.stop();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Output File
Framelayout.xmlFile
frame.xmlFile
<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=".MainActivity"
>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="400dp"
android:text="start" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/stop"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp"
/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/start"
android:layout_alignBottom="@+id/start"
android:layout_alignParentRight="true"
android:layout_marginRight="26dp"
android:text="Stop" />
</RelativeLayout>
frame.java File
package com.example.framebyframeanim;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageView imv;
AnimationDrawable anim;
Button start,stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imv=(ImageView)findViewById(R.id.imageView1);
anim=new AnimationDrawable();
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
imv.setBackgroundResource(R.drawable.aa);
BitmapDrawable f1=(BitmapDrawable)getResources().getDrawable(R.drawable.aa);
BitmapDrawable f2=(BitmapDrawable)getResources().getDrawable(R.drawable.bb);
BitmapDrawable f3=(BitmapDrawable)getResources().getDrawable(R.drawable.cc);
BitmapDrawable f4=(BitmapDrawable)getResources().getDrawable(R.drawable.dd);
BitmapDrawable f5=(BitmapDrawable)getResources().getDrawable(R.drawable.xx);
anim.addFrame(f1, 200);
anim.addFrame(f2, 200);
anim.addFrame(f3, 200);
anim.addFrame(f4, 200);
anim.addFrame(f5, 200);
anim.setOneShot(false);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), "aaaaa", Toast.LENGTH_LONG).show();
imv.setBackgroundDrawable(anim);
anim.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imv.setBackgroundDrawable(anim);
anim.stop();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Output File