Android --- Frame Animation example

Source: Internet
Author: User

Frame Animation:

1. Find a group of images c01.jpg, c02.jpg, c03.jpg, c04.jpg, c05.jpg, and copy them to the res/drawable directory;

2. Create an XML file in the res/drawable Directory: frame_anim.xml


[Html]
<? 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/c01" android: duration = "500"/>
<Item android: drawable = "@ drawable/c02" android: duration = "500"/>
<Item android: drawable = "@ drawable/c03" android: duration = "500"/>
<Item android: drawable = "@ drawable/c04" android: duration = "500"/>
<Item android: drawable = "@ drawable/c05" android: duration = "500"/>
</Animation-list>
<? 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/c01" android: duration = "500"/>
<Item android: drawable = "@ drawable/c02" android: duration = "500"/>
<Item android: drawable = "@ drawable/c03" android: duration = "500"/>
<Item android: drawable = "@ drawable/c04" android: duration = "500"/>
<Item android: drawable = "@ drawable/c05" android: duration = "500"/>
</Animation-list> 3. Create an XML file in the res/layout Directory: frame_anim_layout.xml


[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ImageView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: scaleType = "centerInside"
Android: id = "@ + id/imgFrame"
Android: background = "@ drawable/frame_anim"> </ImageView>
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
<Button
Android: text = ""
Android: id = "@ + id/btnStart"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0" type = "codeph" text = "codeph"> </Button type = "codeph" text = "/codeph">
<Button
Android: text = "end"
Android: id = "@ + id/btnEnd"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0" type = "codeph" text = "codeph"> </Button type = "codeph" text = "/codeph">
</LinearLayout>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ImageView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: scaleType = "centerInside"
Android: id = "@ + id/imgFrame"
Android: background = "@ drawable/frame_anim"> </ImageView>
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
<Button
Android: text = ""
Android: id = "@ + id/btnStart"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0" type = "codeph" text = "codeph"> </Button type = "codeph" text = "/codeph">
<Button
Android: text = "end"
Android: id = "@ + id/btnEnd"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0" type = "codeph" text = "codeph"> </Button type = "codeph" text = "/codeph">
</LinearLayout>
</LinearLayout> 4. Code in the Activity file:


[Java]
Package com. bison;
 
Import android. app. Activity;
Import android. graphics. drawable. AnimationDrawable;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageView;
 
Public class AnimationDemoActivity extends Activity implements OnClickListener {
// Start button
Private Button btnStart;
// End button
Private Button btnEnd;
Private ImageView imgFrame;
// Declare the Frame Animation object
Private AnimationDrawable frameAnim;
 
/** Initialization */
Public void init (){
BtnStart = (Button) findViewById (R. id. btnStart );
BtnEnd = (Button) findViewById (R. id. btnEnd );
BtnStart. setOnClickListener (this );
BtnEnd. setOnClickListener (this );
 
ImgFrame = (ImageView) findViewById (R. id. imgFrame );
// Declare the backgroud of ImageView to the Frame animation object
FrameAnim = (AnimationDrawable) imgFrame. getBackground ();
}
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Load the layout. frame_anim_layout page
SetContentView (R. layout. frame_anim_layout );
Init ();
}
 
Public void onClick (View v ){
// Determine button events
Switch (v. getId ()){
Case R. id. btnStart:
FrameAnim. start ();
Break;
Case R. id. btnEnd:
FrameAnim. stop ();
Break;
}
}
}
Package com. bison;

Import android. app. Activity;
Import android. graphics. drawable. AnimationDrawable;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageView;

Public class AnimationDemoActivity extends Activity implements OnClickListener {
// Start button
Private Button btnStart;
// End button
Private Button btnEnd;
Private ImageView imgFrame;
// Declare the Frame Animation object
Private AnimationDrawable frameAnim;

/** Initialization */
Public void init (){
BtnStart = (Button) findViewById (R. id. btnStart );
BtnEnd = (Button) findViewById (R. id. btnEnd );
BtnStart. setOnClickListener (this );
BtnEnd. setOnClickListener (this );

ImgFrame = (ImageView) findViewById (R. id. imgFrame );
// Declare the backgroud of ImageView to the Frame animation object
FrameAnim = (AnimationDrawable) imgFrame. getBackground ();
}

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Load the layout. frame_anim_layout page
SetContentView (R. layout. frame_anim_layout );
Init ();
}

Public void onClick (View v ){
// Determine button events
Switch (v. getId ()){
Case R. id. btnStart:
FrameAnim. start ();
Break;
Case R. id. btnEnd:
FrameAnim. stop ();
Break;
}
}
}


PS: The Frame animation principle is similar to the film. When a scene or scene passes through, the human's vision changes rapidly during the pause period to form a group chart and generate an animation.

 

From today and now... Bi... Column

Related Article

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.