Android Animations animation effect (2)

Source: Internet
Author: User

The second method of using Animations
1. Create an anim folder under the res folder.
2. Create an xml file and add the set tag first. Modify the tag as follows:

Android: interpolator = "@ android: anim/accelerate_interpolator">

3. Add the rotate, alpha, scale, or translate label to the label.

4. Use AnimationUtils in the code to load the xml file and generate the Animation object.


Below is the code

MainActivity. java

Package com. yx. animations01;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
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. AnimationUtils;
Import android. view. animation. RotateAnimation;
Import android. view. animation. ScaleAnimation;
Import android. view. animation. TranslateAnimation;
Import android. widget. Button;
Import android. widget. ImageView;

Public class MainActivity extends Activity {

Private Button scaleButton = null;
Private Button rotateButton = null;
Private Button alphaButton = null;
Private Button translateButton = null;
Private ImageView imageView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

ScaleButton = (Button) findViewById (R. id. scaleButton );
ScaleButton. setOnClickListener (new scaleButtonListener ());

RotateButton = (Button) findViewById (R. id. rotateButton );
RotateButton. setOnClickListener (new rotateButtonListener ());

AlphaButton = (Button) findViewById (R. id. alphaButton );
AlphaButton. setOnClickListener (new alphaButtonListener ());

TranslateButton = (Button) findViewById (R. id. translateButton );
TranslateButton. setOnClickListener (new translateButtonListener ());

ImageView = (ImageView) findViewById (R. id. imageViewId );
}

Class scaleButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. scale );
ImageView. startAnimation (animation );
}
}

Class rotateButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. rotate );
ImageView. startAnimation (animation );
}
}

// Fade in and out
Class alphaButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// Use AnimationUtils to load the animation settings file
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. alpha );
ImageView. startAnimation (animation );
}
}

Class translateButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. translate );
ImageView. startAnimation (animation );
}
}

}

The following are four xml

Alpha. xml


Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: toAlpha = "0.0"
Android: startOffset = "500"
Android: duration = "500">


Rotate. xml


Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: toDegrees = "+ 350"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration = "3000">



Scale. xml


Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: fromXScale = "1.0"
Android: toXScale = "0.0"
Android: fromYScale = "1.0"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration= "2000"
>


Translate. xml


Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: toXDelta = "100%"
Android: fromYDelta = "0%"
Android: toYDelta = "100%"
Android: duration= "2000"
/>

Note the xml file placement location


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.