MainActivity
Java code
Package org. wp. activity;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. ImageView;
Public class MainActivity extends Activity {
Private ImageView myImageView = null;
Private Button myButton = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MyImageView = (ImageView) this. findViewById (R. id. myImageView );
MyButton = (Button) this. findViewById (R. id. myButton );
MyButton. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
MyImageView. startAnimation (new TVOffAnimation ());
}
});
}
}
TVOffAnimation
Java code
Package org. wp. activity;
Import android. graphics. Matrix;
Import android. view. animation. AccelerateDecelerateInterpolator;
Import android. view. animation. Animation;
Import android. view. animation. Transformation;
Public class TVOffAnimation extends Animation {
/** Center X coordinate **/
Private int centerX = 0;
/** Y coordinate of the center **/
Private int centerY = 0;
@ Override
Public void initialize (int width, int height, int parentWidth,
Int parentHeight ){
// Void setDuration (long durationMillis)
// Since: API Level 1 How long this animation last.
// The duration cannot be negative.
SetDuration (500 );
// Void setFillAfter (boolean fillAfter)
// If fillAfter is true, the transformation that this animation
// Specified Med will persist when it is finished.
SetFillAfter (true );
// Obtain the X coordinate of the image center.
CenterX = width/2;
// Obtain the Y coordinate of the image center.
CenterY = height/2;
// Void setInterpolator (Interpolator I)
// Since: API Level 1 Sets the acceleration curve for this animation.
// Defaults to a linear interpolation.
// SetInterpolator (new AccelerateDecelerateInterpolator ())
// Select a Speed Effect
// AccelerateDecelerateInterpolator
// An interpolator where the rate of change starts and ends slowly
// But accelerates through the middle.
SetInterpolator (new AccelerateDecelerateInterpolator ());
}
/**
* PreScale (float sx, float sy, float px, float py)
* Px and py are fixed points,
* For example, if px, py = 0,
* The image is scaled down to the right based on the upper left corner.
*
* If the image is centered, the image is taken as the base point,
* Scale up or down to the upper, lower, left, and other proportions.
*
* Generally, if the internal coordinates of an image are not important,
* Only use preScale (sy, sy.
* In the case of px and py, it usually involves the operation of Animation before and after.
*
* Simply put, the zoom-in ratio does not change, but is determined by sx and sy.
* Only the pixel and py points remain unchanged before and after amplification.
*
* For example, a rectangle (width) 20 (height) 10,
* The coordinates in the upper left corner are (0, 0 ). The bottom right corner is (20, 10 ).
* If sx, sy is 2 or 2, it is zoomed twice.
* When px, py = is enlarged, the upper left corner is still (), but the lower right corner is changed ).
*
* But also sx, sy = 2, but px, py = 10, 5,
* After the image is enlarged, the upper left corner is (-10,-5), and the lower right corner is ).
* The unique coordinates remain unchanged at 10 or 5. But the rectangle is still magnified by two times.
*
* It seems similar, but if you use Animation,
* Because Animation requires coordinates, the effects may vary.
*
*
* InterpolatedTime indicates that the interval between the current animation and animation is 0-1.
*
* We Need To horizontally stretch the first 80% of the time to 150%,
* The change rate is 0.5/0.8 = 0.625.
* The horizontal scaling value is 1 + 0.625f * interpolatedTime
*
* The first 80% of the vertical direction is directly reduced, and only one line with a height of 0.01f is left.
* The change rate is 1/0.8 = 1.25
* The vertical scaling value is 1-1.25f * interpolatedTime + 0.01f
* Of course, you can also write 1-interpolatedTime/0.8f + 0.01f
*
* In the next 20% of the time, we need to horizontally compress from 150% to 0%,
* The change rate is 1.5/0.2 = 7.5.
* The horizontal scaling value is 7.5f * (1-interpolatedTime)
*
* Keep the vertical orientation unchanged. When the horizontal orientation is 0, all will disappear.
*/
@ Override
Protected void applyTransformation (float interpolatedTime, Transformation t ){
Final Matrix matrix = t. getMatrix ();
If (interpolatedTime <0.8 ){
Matrix. preScale (1 + 0.625f * interpolatedTime,
1-1.25f * interpolatedTime + 0.01f, centerX, centerY );
} Else {
Matrix. preScale (7.5f * (1-interpolatedTime), 0.01f,
CenterX, centerY );
}
}
}
Main. xml
Xml Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
Android: gravity = "center">
<ImageView android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/bg"
Android: id = "@ + id/myImageView"/>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "20dip"
Android: text = "power off"
Android: id = "@ + id/myButton"/>
</LinearLayout>
Author "onewayonelife"