Frame Animations in Android

Source: Internet
Author: User

Mainactivity file:

public class Mainactivity extends Activity implements onclicklistener{
Animationdrawable Anim_draw;
@SuppressLint ("Newapi")
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
ImageView Anim_iv = (ImageView) Findviewbyid (R.id.iv_anim);
Anim_draw = new Animationdrawable ();
Anim_draw.addframe (Getresources (). Getdrawable (r.drawable.animate_straight_zuzi0), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.DRAWABLE.ANIMATE_STRAIGHT_ZUZI1), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.DRAWABLE.ANIMATE_STRAIGHT_ZUZI2), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.DRAWABLE.ANIMATE_STRAIGHT_ZUZI3), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.DRAWABLE.ANIMATE_STRAIGHT_ZUZI4), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.DRAWABLE.ANIMATE_STRAIGHT_ZUZI5), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.DRAWABLE.ANIMATE_STRAIGHT_ZUZI6), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.DRAWABLE.ANIMATE_STRAIGHT_ZUZI7), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.DRAWABLE.ANIMATE_STRAIGHT_ZUZI8), 100);
Anim_draw.addframe (Getresources (). Getdrawable (R.drawable.animate_straight_zuzi9), 100);
Anim_draw.setoneshot (FALSE);
Anim_iv.setbackground (Anim_draw);
Anim_iv.setbackgroundresource (r.drawable.anim_list);
Anim_draw = (animationdrawable) anim_iv.getbackground ();
Findviewbyid (R.id.btn_start). Setonclicklistener (this);
Findviewbyid (r.id.btn_stop). Setonclicklistener (this);
}
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.btn_start:
Anim_draw.start ();
Break
Case R.id.btn_stop:
Anim_draw.stop ();
Break
}
}
}

Activity_main 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"
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= ". Mainactivity ">

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<button
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/btn_start"
android:text= "Start"/>
<button
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/btn_stop"
android:text= "Stop"/>
<relativelayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<imageview
Android:layout_width= "200DP"
android:layout_height= "200DP"
Android:layout_centerinparent= "true"
Android:id= "@+id/iv_anim"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>

Anim_list 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"
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= ". Mainactivity ">

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<button
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/btn_start"
android:text= "Start"/>
<button
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/btn_stop"
android:text= "Stop"/>
<relativelayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<imageview
Android:layout_width= "200DP"
android:layout_height= "200DP"
Android:layout_centerinparent= "true"
Android:id= "@+id/iv_anim"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>

Final effect:

Frame Animations in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.