Android implements simple translation, rotation, scaling, and transparency animation by using Animate hard encoding.

Source: Internet
Author: User

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;
}

}
};


 


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.