Only the main code is listed below:
[Java]
Private ImageView scanLight;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
ScanLight = (ImageView) findViewById (R. id. img );
Button translateStart = (Button) findViewById (R. id. translateStart );
Button translateEnd = (Button) findViewById (R. id. translateEnd );
Button scaleStart = (Button) findViewById (R. id. scaleStart );
Button scaleEnd = (Button) findViewById (R. id. scaleEnd );
Button alphaStart = (Button) findViewById (R. id. alphaStart );
Button alphaEnd = (Button) findViewById (R. id. alphaEnd );
Button rotateStart = (Button) findViewById (R. id. rotateStart );
Button rotateEnd = (Button) findViewById (R. id. rotateEnd );
TranslateStart. setOnClickListener (listener );
TranslateEnd. setOnClickListener (listener );
ScaleStart. setOnClickListener (listener );
ScaleEnd. setOnClickListener (listener );
AlphaStart. setOnClickListener (listener );
AlphaEnd. setOnClickListener (listener );
RotateStart. setOnClickListener (listener );
RotateEnd. setOnClickListener (listener );
}
Private OnClickListener listener = new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
Switch (v. getId ())
{
// The setFillAfter (true) Translation controls the animation to be in the current State after it is executed.
Case R. id. translateStart:
Animation translateIn = new TranslateAnimation (0,100, 0, 0 );
TranslateIn. setDuration (500 );
TranslateIn. setFillAfter (true );
ScanLight. startAnimation (translateIn );
Break;
Case R. id. translateEnd:
Animation translateOut = new TranslateAnimation (100, 0, 0, 0 );
TranslateOut. setDuration (500 );
TranslateOut. setFillAfter (true );
ScanLight. startAnimation (translateOut );
Break;
// The last four parameters control zooming along the center
Case R. id. scaleStart:
Animation sIn = new ScaleAnimation (1f, 2f, 1f, 2f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
SIn. setDuration (500 );
SIn. setFillAfter (true );
ScanLight. startAnimation (sIn );
Break;
Case R. id. scaleEnd:
Animation sOut = new ScaleAnimation (2f, 1f, 2f, 1f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
SOut. setDuration (500 );
SOut. setFillAfter (true );
ScanLight. startAnimation (sOut );
Break;
// Transparency
Case R. id. alphaStart:
Animation aIn = new AlphaAnimation (1f, 0f );
AIn. setDuration (500 );
AIn. setFillAfter (true );
ScanLight. startAnimation (aIn );
Break;
Case R. id. alphaEnd:
Animation aOut = new AlphaAnimation (0f, 1f );
AOut. setDuration (500 );
AOut. setFillAfter (true );
ScanLight. startAnimation (aOut );
Break;
// The four parameters after rotation control rotation along the center
Case R. id. rotateStart:
Animation rIn = new RotateAnimation (0f, + 360f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
RIn. setDuration (500 );
RIn. setFillAfter (true );
ScanLight. startAnimation (rIn );
Break;
Case R. id. rotateEnd:
Animation rOut = new RotateAnimation (+ 360f, 0f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
ROut. setDuration (500 );
ROut. setFillAfter (true );
ScanLight. startAnimation (rOut );
Break;
Default:
Break;
}
}
};
Private ImageView scanLight;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
ScanLight = (ImageView) findViewById (R. id. img );
Button translateStart = (Button) findViewById (R. id. translateStart );
Button translateEnd = (Button) findViewById (R. id. translateEnd );
Button scaleStart = (Button) findViewById (R. id. scaleStart );
Button scaleEnd = (Button) findViewById (R. id. scaleEnd );
Button alphaStart = (Button) findViewById (R. id. alphaStart );
Button alphaEnd = (Button) findViewById (R. id. alphaEnd );
Button rotateStart = (Button) findViewById (R. id. rotateStart );
Button rotateEnd = (Button) findViewById (R. id. rotateEnd );
TranslateStart. setOnClickListener (listener );
TranslateEnd. setOnClickListener (listener );
ScaleStart. setOnClickListener (listener );
ScaleEnd. setOnClickListener (listener );
AlphaStart. setOnClickListener (listener );
AlphaEnd. setOnClickListener (listener );
RotateStart. setOnClickListener (listener );
RotateEnd. setOnClickListener (listener );
}
Private OnClickListener listener = new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
Switch (v. getId ())
{
// The setFillAfter (true) Translation controls the animation to be in the current State after it is executed.
Case R. id. translateStart:
Animation translateIn = new TranslateAnimation (0,100, 0, 0 );
TranslateIn. setDuration (500 );
TranslateIn. setFillAfter (true );
ScanLight. startAnimation (translateIn );
Break;
Case R. id. translateEnd:
Animation translateOut = new TranslateAnimation (100, 0, 0, 0 );
TranslateOut. setDuration (500 );
TranslateOut. setFillAfter (true );
ScanLight. startAnimation (translateOut );
Break;
// The last four parameters control zooming along the center
Case R. id. scaleStart:
Animation sIn = new ScaleAnimation (1f, 2f, 1f, 2f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
SIn. setDuration (500 );
SIn. setFillAfter (true );
ScanLight. startAnimation (sIn );
Break;
Case R. id. scaleEnd:
Animation sOut = new ScaleAnimation (2f, 1f, 2f, 1f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
SOut. setDuration (500 );
SOut. setFillAfter (true );
ScanLight. startAnimation (sOut );
Break;
// Transparency
Case R. id. alphaStart:
Animation aIn = new AlphaAnimation (1f, 0f );
AIn. setDuration (500 );
AIn. setFillAfter (true );
ScanLight. startAnimation (aIn );
Break;
Case R. id. alphaEnd:
Animation aOut = new AlphaAnimation (0f, 1f );
AOut. setDuration (500 );
AOut. setFillAfter (true );
ScanLight. startAnimation (aOut );
Break;
// The four parameters after rotation control rotation along the center
Case R. id. rotateStart:
Animation rIn = new RotateAnimation (0f, + 360f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
RIn. setDuration (500 );
RIn. setFillAfter (true );
ScanLight. startAnimation (rIn );
Break;
Case R. id. rotateEnd:
Animation rOut = new RotateAnimation (+ 360f, 0f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
ROut. setDuration (500 );
ROut. setFillAfter (true );
ScanLight. startAnimation (rOut );
Break;
Default:
Break;
}
}
};