1. Create an XML file in the layout directory:
[Html]
<Span style = "font-size: 18px;"> <? 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: id = "@ + id/imgTween"
Android: src = "@ drawable/c01"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_weight = "1.0"/>
<Button
Android: id = "@ + id/btnControl"
Android: text = ""
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
</Span>
<Span style = "font-size: 18px;"> <? 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: id = "@ + id/imgTween"
Android: src = "@ drawable/c01"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_weight = "1.0"/>
<Button
Android: id = "@ + id/btnControl"
Android: text = ""
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
</Span> 2. Write in the Activity code:
[Java]
<Span style = "font-size: 18px;"> package com. bison;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. animation. AlphaAnimation;
Import android. view. animation. Animation;
Import android. view. animation. AnimationSet;
Import android. view. animation. RotateAnimation;
Import android. view. animation. ScaleAnimation;
Import android. view. animation. TranslateAnimation;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. widget. ImageView. ScaleType;
Public class TweenAnimationDemo extends Activity implements OnClickListener {
// Declare an identifier for starting to stop
Private boolean flags = true;
Private ImageView imgTween;
Private Button btnCtrl;
Private AnimationSet;
/** Initialization */
Public void init (){
// Declare the AnimationSet
As = new AnimationSet (true );
// Declare animations such as Alpha, Scale, Translate, and Rotate
AlphaAnimation aa = alphaAnim (1, 0.3f );
ScaleAnimation sa = scaleAnim (0.2f, 1.0f, 0.2f, 1.0f, 1, 1 );
TranslateAnimation ta = translateAnim (50f, 100f, 50f, 100f );
RotateAnimation ra = rotateAnim (0,360 );
// Add various animations
As. addAnimation (aa );
As. addAnimation (sa );
As. addAnimation (ta );
As. addAnimation (ra );
ImgTween = (ImageView) findViewById (R. id. imgTween );
ImgTween. setScaleType (ScaleType. CENTER_INSIDE );
BtnCtrl = (Button) findViewById (R. id. btnControl );
BtnCtrl. setOnClickListener (this );
}
/** Zoom */
Private ScaleAnimation scaleAnim (float start_x, float end_x, float start_y,
Float end_y, float x2, float y2 ){
// Start x coordinate scaling size, end x coordinate scaling size, start y coordinate scaling size, end y coordinate scaling size, x axis percentage, y axis percentage
ScaleAnimation sa = new ScaleAnimation (start_x, end_x, start_y, end_y,
X2, y2 );
Sa. setDuration (3000 );
Sa. setRepeatMode (Animation. REVERSE );
Sa. setRepeatCount (5 );
Return sa;
}
/** Transparency */
Private AlphaAnimation alphaAnim (float x, float y ){
AlphaAnimation aa = new AlphaAnimation (x, y );
Aa. setDuration (2000 );
Aa. setRepeatMode (Animation. REVERSE );
Aa. setRepeatCount (5 );
Return aa;
}
/** Move */
Private TranslateAnimation translateAnim (float startX, float endX,
Float startY, float endY ){
TranslateAnimation ta = new TranslateAnimation (startX, endX, startY,
EndY );
Ta. setDuration (3000 );
Ta. setRepeatMode (Animation. REVERSE );
Ta. setRepeatCount (5 );
Return ta;
}
/** Rotate */
Private RotateAnimation rotateAnim (float startDegrees, float endDegrees ){
RotateAnimation ra = new RotateAnimation (startDegrees, endDegrees );
Ra. setDuration (3000 );
Ra. setRepeatMode (Animation. RESTART );
Ra. setRepeatCount (5 );
Return ra;
}
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. tween_anim_layout );
Init ();
}
Public void onClick (View v ){
If (flags ){
BtnCtrl. setText ("stop ");
ImgTween. startAnimation ();
Flags = false;
} Else {
BtnCtrl. setText ("START ");
ImgTween. clearAnimation ();
Flags = true;
}
}
}
</Span>
<Span style = "font-size: 18px;"> package com. bison;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. animation. AlphaAnimation;
Import android. view. animation. Animation;
Import android. view. animation. AnimationSet;
Import android. view. animation. RotateAnimation;
Import android. view. animation. ScaleAnimation;
Import android. view. animation. TranslateAnimation;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. widget. ImageView. ScaleType;
Public class TweenAnimationDemo extends Activity implements OnClickListener {
// Declare an identifier for starting to stop
Private boolean flags = true;
Private ImageView imgTween;
Private Button btnCtrl;
Private AnimationSet;
/** Initialization */
Public void init (){
// Declare the AnimationSet
As = new AnimationSet (true );
// Declare animations such as Alpha, Scale, Translate, and Rotate
AlphaAnimation aa = alphaAnim (1, 0.3f );
ScaleAnimation sa = scaleAnim (0.2f, 1.0f, 0.2f, 1.0f, 1, 1 );
TranslateAnimation ta = translateAnim (50f, 100f, 50f, 100f );
RotateAnimation ra = rotateAnim (0,360 );
// Add various animations
As. addAnimation (aa );
As. addAnimation (sa );
As. addAnimation (ta );
As. addAnimation (ra );
ImgTween = (ImageView) findViewById (R. id. imgTween );
ImgTween. setScaleType (ScaleType. CENTER_INSIDE );
BtnCtrl = (Button) findViewById (R. id. btnControl );
BtnCtrl. setOnClickListener (this );
}
/** Zoom */
Private ScaleAnimation scaleAnim (float start_x, float end_x, float start_y,
Float end_y, float x2, float y2 ){
// Start x coordinate scaling size, end x coordinate scaling size, start y coordinate scaling size, end y coordinate scaling size, x axis percentage, y axis percentage
ScaleAnimation sa = new ScaleAnimation (start_x, end_x, start_y, end_y,
X2, y2 );
Sa. setDuration (3000 );
Sa. setRepeatMode (Animation. REVERSE );
Sa. setRepeatCount (5 );
Return sa;
}
/** Transparency */
Private AlphaAnimation alphaAnim (float x, float y ){
AlphaAnimation aa = new AlphaAnimation (x, y );
Aa. setDuration (2000 );
Aa. setRepeatMode (Animation. REVERSE );
Aa. setRepeatCount (5 );
Return aa;
}
/** Move */
Private TranslateAnimation translateAnim (float startX, float endX,
Float startY, float endY ){
TranslateAnimation ta = new TranslateAnimation (startX, endX, startY,
EndY );
Ta. setDuration (3000 );
Ta. setRepeatMode (Animation. REVERSE );
Ta. setRepeatCount (5 );
Return ta;
}
/** Rotate */
Private RotateAnimation rotateAnim (float startDegrees, float endDegrees ){
RotateAnimation ra = new RotateAnimation (startDegrees, endDegrees );
Ra. setDuration (3000 );
Ra. setRepeatMode (Animation. RESTART );
Ra. setRepeatCount (5 );
Return ra;
}
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. tween_anim_layout );
Init ();
}
Public void onClick (View v ){
If (flags ){
BtnCtrl. setText ("stop ");
ImgTween. startAnimation ();
Flags = false;
} Else {
BtnCtrl. setText ("START ");
ImgTween. clearAnimation ();
Flags = true;
}
}
}
</Span>
PS: This method is better than defined in XML. It can pass parameters, modify parameters, and so on, making it easier to operate.
From today and now... Bi... Column