Android development-graphic images and animations (2) Animation for gradient, scaling, displacement, and rotation of images

Source: Internet
Author: User

The Android platform provides two types of animations. One is Tween animation, which is used to continuously change the images of objects in the scene to produce the animation effect (rotation, translation, contraction, and gradient ).
Next, let's talk about Tweene Animations.
Main Category:
Animation
AlphaAnimation gradient transparency
RotateAnimation image rotation
ScaleAnimation gradient Scaling
TranslateAnimation location Movement
Animation set

1. AlphaAnimation
The first parameter fromAlpha in the AlphaAnimation class indicates the transparency at the animation start, and the second parameter toAlpha indicates the transparency at the animation end.
SetDuration is used to set the animation duration.

Ii. RotateAnimation
The first parameter fromDegrees in the RotateAnimation class indicates the angle of the animation starting from, and the second parameter toDegrees indicates the angle of the animation ending.

In addition, you can also set the scaling modes such as pivotXType and pivotYType. The starting positions of the scaling animation, such as pivotXValue and pivotYValue, are relative to x and y coordinates.

Iii. ScaleAnimation
In the ScaleAnimation class
The first parameter fromX and the second parameter toX are the scaling sizes on the X coordinate at the start and end of the animation respectively.
The third parameter fromY and the fourth parameter toY are the scaling dimensions on the Y coordinate at the start and end of the animation respectively.
In addition, you can also set the scaling modes such as pivotXType and pivotYType. The starting positions of the scaling animation, such as pivotXValue and pivotYValue, are relative to x and y coordinates.

Iv. TranslateAnimation
The first parameter fromXDelta and the second parameter toXDelta are the X coordinates at the animation start and end.
The third parameter fromYDelta and the fourth parameter toYDelta are the Y coordinates at the animation start and end.
The example I implemented below is to make the image have the above four animation effects, and the fifth implementation is the overlapping of the two effects.As follows::

Click each button to respond accordingly.
The layout file used in this example is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<AbsoluteLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Xmlns: android = "http://schemas.android.com/apk/res/android">
<Button
Android: id = "@ + id/button_scale"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "scale"
Android: layout_x = "5dp"
Android: layout_y = "383dp"/>
<Button
Android: id = "@ + id/button_rotate"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "rotate"
Android: layout_x = "158dp"
Android: layout_y = "383dp"/>
<Button
Android: id = "@ + id/button_alpha"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "alpha"
Android: layout_x = "5dp"
Android: layout_y = "331dp"/>
<Button
Android: id = "@ + id/button_translate"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "translate"
Android: layout_x = "160dp"
Android: layout_y = "329dp"/>
<Button
Android: id = "@ + id/button_alpha_translate"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "alpha_translate"
Android: layout_x = "84dp"
Android: layout_y = "265dp"/>
<ImageView
Android: id = "@ + id/imageview"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_x = "105dp"
Android: layout_y = "133dp"
Android: src = "@ drawable/ic_launcher"
/>
</AbsoluteLayout>

The source code for implementing this instance is as follows:Copy codeThe Code is as follows: public class Animations_Activity extends Activity {
Private Button button1;
Private Button button2;
Private Button button3;
Private Button button4;
Private Button button5;
Private ImageView imageView;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_animations _);
Button1 = (Button) findViewById (R. id. button_alpha );
Button2 = (Button) findViewById (R. id. button_rotate );
Button3 = (Button) findViewById (R. id. button_scale );
Button4 = (Button) findViewById (R. id. button_translate );
Button5 = (Button) findViewById (R. id. button_alpha_translate );
ImageView = (ImageView) findViewById (R. id. imageview );
Button1.setOnClickListener (new MyButton ());
Button2.setOnClickListener (new MyButton ());
Button3.setOnClickListener (new MyButton ());
Button4.setOnClickListener (new MyButton ());
Button5.setOnClickListener (new MyButton ());
}
Class MyButton implements OnClickListener {
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Switch (arg0.getId ()){
Case R. id. button_alpha:
Alpha ();
Break;
Case R. id. button_rotate:
Rotata ();
Break;
Case R. id. button_scale:
Scale ();
Break;
Case R. id. button_translate:
Translate ();
Break;
Case R. id. button_alpha_translate:
Alpha_Translate ();
Break;
Default:
Break;
}
}

}

/*
* 1. Create an AnimationSet object that stores an animation set.
* 2. Create an Animation object as needed.
* 3. Set the corresponding data (Execution effect) for the Animation object based on the Animation requirements)
* 4. Add the Animation object to the Animation set object.
* 5. Use the control object to start executing the AnimationSet
*/
Public void Alpha (){
AnimationSet animationSet = new AnimationSet (true );
AlphaAnimation alphaAnimation = new AlphaAnimation (1, 0 );
AlphaAnimation. setDuration (2000 );
AnimationSet. addAnimation (alphaAnimation );
ImageView. startAnimation (animationSet );
}
Public void Rotata (){
AnimationSet animationSet = new AnimationSet (true );
// The following four parameters define the center of the rotation
RotateAnimation rotateAnimation = new RotateAnimation (
0,360,
Animation. RELATIVE_TO_PARENT, 1f,
Animation. RELATIVE_TO_PARENT, 0f );
RotateAnimation. setDuration (2000 );
AnimationSet. addAnimation (rotateAnimation );
ImageView. startAnimation (animationSet );
}
Public void Scale (){
AnimationSet animationSet = new AnimationSet (true );
ScaleAnimation scaleAnimation = new ScaleAnimation (
1, 0.1f, 1, 0.1f,
Animation. RELATIVE_TO_SELF, 0.5f,
Animation. RELATIVE_TO_SELF, 0.5f );
ScaleAnimation. setDuration (2000 );
AnimationSet. addAnimation (scaleAnimation );
ImageView. startAnimation (scaleAnimation );
}
Public void Translate (){
AnimationSet animationSet = new AnimationSet (true );
TranslateAnimation translateAnimation = new TranslateAnimation (
Animation. RELATIVE_TO_SELF, 0f, // start position of the X axis
Animation. RELATIVE_TO_SELF, 0.5f, // end position of the X axis
Animation. RELATIVE_TO_SELF, 0f, // start position of the Y axis
Animation. RELATIVE_TO_SELF, 1.0f); // end position of the Y axis
TranslateAnimation. setDuration (2000 );
AnimationSet. addAnimation (translateAnimation );

/*
* If the first row is set to true, the animation is frozen after execution.
* If the value of the second row is set to false, the animation is frozen after execution.
* The third row sets a long value, which is the number of milliseconds after the animation is executed.
* The fourth row defines how many times the animation is repeatedly executed.
*/
AnimationSet. setFillAfter (true );
AnimationSet. setFillBefore (false );
AnimationSet. setStartOffset (2000 );
AnimationSet. setRepeatCount (3 );

ImageView. startAnimation (animationSet );
}
Public void Alpha_Translate (){
AnimationSet animationSet = new AnimationSet (true );
AlphaAnimation alphaAnimation = new AlphaAnimation (1, 0 );
AlphaAnimation. setDuration (2000 );
AnimationSet. addAnimation (alphaAnimation );
TranslateAnimation translateAnimation = new TranslateAnimation (
Animation. RELATIVE_TO_SELF, 0f, // start position of the X axis
Animation. RELATIVE_TO_SELF, 0.5f, // end position of the X axis
Animation. RELATIVE_TO_SELF, 0f, // start position of the Y axis
Animation. RELATIVE_TO_SELF, 1.0f); // end position of the Y axis
TranslateAnimation. setDuration (2000 );
AnimationSet. addAnimation (translateAnimation );
ImageView. startAnimation (animationSet );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. activity_animations _, menu );
Return true;
}
}

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.