In the below example code. I have created Frame animation in android. Here I have created a Textview in activity_main.xml layout. After then I have created a new framanim.xml layout in drawable folder, In framanim.xml layout I have added four image items. In MainActivity I have used AnimationDrawable class and getResources() method. You can see below example code it clearly describe you How to create Frame animation in android.
Step(1)actvity_main.xml layout-
<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:background="#3f51b5"
android:id="@+id/mainView1"
tools:context="andriod.frame.by.frame.animation.MainActivity" >
<TextView
android:id="@+id/imageView1"
android:layout_width="210dp"
android:layout_height="50dp"
android:text="RajShekhar"
android:textColor="#fd471a"
android:gravity="center"
android:textSize="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Step(2)- I have created a new framanim.xml layout -
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/one"
android:duration="2000" />
<item
android:drawable="@drawable/two"
android:duration="2000" />
<item
android:drawable="@drawable/three"
android:duration="2000" />
</animation-list>
Step(3)-MainActivity-
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AnimationDrawable ad = (AnimationDrawable) getResources().getDrawable(R.drawable.framanim);
View iv1 = (View) findViewById(R.id.mainView1);
iv1.setBackgroundDrawable(ad);
ad.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
0 Comment(s)