Use of Animations in android Development (2)

Source: Internet
Author: User

Use of Animations in android Development (2)

Use of Animations in android Development (2)

This blog post focuses on the use of animation effects in android development. Unlike the previous blog post, the four animation effects are mainly implemented using xml files, this improves code reusability and maintainability.

The basic steps are as follows:

1. First, create an anim folder under the res folder
2. Create four new animated xml files (scale. xml, rotate. xml, alpha. xml, and translate. xml) in this folder)
3. directly use AnimationUtils in the code to call the static method loadAnimation to load and create an animation object.
4. Run the animation object


The instance code is as follows:

Package com. example. animationtest2;


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. Animation;
Import android. view. animation. AnimationUtils;
Import android. widget. Button;
Import android. widget. ImageView;




Public class MainActivity extends Activity {


Private ImageView imageView = null;
Private Button scaleButton = null;
Private Button translateButton = null;
Private Button rotateButton = null;
Private Button alphaButton = null;



@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

ImageView = (ImageView) findViewById (R. id. myImage );
ScaleButton = (Button) findViewById (R. id. scaleButton );
TranslateButton = (Button) findViewById (R. id. translateButton );
RotateButton = (Button) findViewById (R. id. rotateButton );
AlphaButton = (Button) findViewById (R. id. alphaButton );

ScaleButton. setOnClickListener (new setScaleListener ());
TranslateButton. setOnClickListener (new setTranslateListener ());
RotateButton. setOnClickListener (new setRotateListener ());
AlphaButton. setOnClickListener (new setAlphaListener ());

}

// Animation scaling effect listener
Class setScaleListener implements OnClickListener {


@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. scale );

ImageView. startAnimation (animation );
}

}

// Animation movement effect listener
Class setTranslateListener implements OnClickListener {


@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. translate );

ImageView. startAnimation (animation );
}

}

// Rotating the animation effect listener
Class setRotateListener implements OnClickListener {


@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. rotate );

ImageView. startAnimation (animation );
}

}


// Gets the animation effect listener in and out.
Class setAlphaListener implements OnClickListener {


@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. alpha );

ImageView. startAnimation (animation );
}

}





@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}


}




Main. xml:

Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">


Android: id = "@ + id/myText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>

Android: id = "@ + id/imgLayout"
Android: layout_below = "@ id/myText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: padding = "55dp"
>
Android: id = "@ + id/myImage"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/ic_launcher"
/>



Android: id = "@ + id/scaleButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/imgLayout"
Android: text = "@ string/scale"
/>

Android: id = "@ + id/translateButton"
Android: layout_below = "@ id/scaleButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/translate"
/>

Android: id = "@ + id/rotateButton"
Android: layout_below = "@ id/translateButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/rotate"
/>

Android: id = "@ + id/alphaButton"
Android: layout_below = "@ id/rotateButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/alpha"
/>








Xml files of the four animation effects are as follows:

Scale. xml:



Android: fromXScale = "1"
Android: toXScale = "0"
Android: fromYScale = "1"
Android: toYScale = "0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration= "2000"
/>





Rotate. xml:



Android: fromDegrees = "0"
Android: toDegrees = "-360"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration= "2000"
/>






Alpha. xml:




Android: fromalphi = "1.0"
Android: toAlpha = "0.1"
Android: duration= "2000"
/>


Translate. xml:



Android: fromxdel= "0%"
Android: toXDelta = "50%"
Android: fromYDelta = "0%"
Android: toYDelta = "100%"
Android: duration= "2000"
/>







The implementation result is as follows:



Click the related button to view the effect



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.