Animation implementation Function Description: similar to the gallery function, click a thumbnail of an image, and then open the image in full screen. The intermediate process is implemented through animation, this gives the operator a feeling of enlarging the thumbnail to full screen.
The above description shows that the position of the thumbnail clicked by the user is not fixed, so the starting position of the animation is not fixed, in addition, in some special cases, the animation may start with an unfixed view size, so it is impossible to use anim's XML definition, so here I will use the hand-written animationset method to implement this animation.
ExampleProgramDescription:
In this example, you can click a button at a different position and then use an animation to bring up a linearlayout. Click the linearlayout button and then use an animation to hide the linearlayout.
Step 1: Create a project and add multiple buttons to the initial main. xml file.
In this example, six buttons are added.CodeIt will no longer be posted. It looks like this:
Step 2: Add a click event to each button
You can declare the click event through the Android: onclick = "btnonclick" attribute in XML, and then implement this method on the corresponding activity. The Code is as follows:
Btnonclick (view BTN)
1Public VoidBtnonclick (view BTN ){2 Showview (BTN );3}
Step 3: Bring up and hide animations
The animation is shown as follows:
Show animation
1 Animationset animset = New Animationset ( True ); 2 Scaleanimation SA = New Scaleanimation ((Float ) V. getwidth () 3 /(View) v. getparent (). getwidth (), 1.0f , 4 ( Float ) V. getheight ()/ (View) v. getparent (). getheight (), 5 1.0f, V. getx () + V. getwidth ()/2, V. Gety () + V. getheight ()/2 ); 6 SA. setduration (2000 ); 7 Alphaanimation AA = New Alphaanimation (0.2f, 1 ); 8 AA. setduration (2000 ); 9 Animset. addanimation (SA ); 10 Animset. addanimation (AA );
The hidden animation is as follows:
Hide an animation
1 Animationset animset = New Animationset ( True ); 2 Scaleanimation SA = New Scaleanimation (1 ,( Float ) V. getwidth () 3 /(View) v. getparent (). getwidth (), 1 ,( Float ) V. getheight () 4 /(View) v. getparent (). getheight (), V. getx () + V. getwidth () 5 /2, V. Gety () + V. getheight ()/2 ); 6 SA. setduration (2000 ); 7 Alphaanimation AA = New Alphaanimation (1f, 0f ); 8 AA. setduration (2000 ); 9 Animset. addanimation (SA ); 10 Animset. addanimation (AA );
To sum up, the core code of the entire example is the two methods of activity.Source codeAs follows:
Activity
1 Public Class Animtestactivity Extends Activity { 2 Private Linearlayout mview = Null ; 3 4 /** Called when the activity is first created. */ 5 @ Override 6 Public Void Oncreate (bundle savedinstancestate ){ 7 Super . Oncreate (savedinstancestate ); 8 Setcontentview (R. layout. Main ); 9 } 10 11 Public Void Btnonclick (view BTN ){ 12 Showview (BTN ); 13 } 14 15 Private Void Showview ( Final View v ){ 16 If (Mview = Null ){ 17 Mview = New Linearlayout (This ); 18 Mview. setbackgroundcolor (color. Blue ); 19 Addcontentview (mview, New Viewgroup. layoutparams ( 20 Layoutparams. fill_parent, layoutparams. fill_parent )); 21 } 22 Mview. setonclicklistener ( New View. onclicklistener (){ 23 @ Override 24 Public Void Onclick (view ){ 25 Hideview (v ); 26 } 27 }); 28 29 Animationset animset =New Animationset ( True ); 30 Scaleanimation SA = New Scaleanimation (( Float ) V. getwidth () 31 /(View) v. getparent (). getwidth (), 1.0f , 32 ( Float ) V. getheight ()/ (View) v. getparent (). getheight (), 33 1.0f, V. getx () + V. getwidth ()/2, V. Gety () + V. getheight ()/2 ); 34 SA. setduration (2000 ); 35 Alphaanimation AA = New Alphaanimation (0.2f, 1 ); 36 AA. setduration (2000 ); 37 Animset. addanimation (SA ); 38 Animset. addanimation (AA ); 39 Mview. startanimation (animset ); 40 Mview. setvisibility (view. Visible ); 41 } 42 43 Private Void Hideview (view v ){ 44 Animationset animset = New Animationset ( True ); 45 Scaleanimation SA = New Scaleanimation (1 ,( Float ) V. getwidth () 46 /(View) v. getparent (). getwidth (), 1 ,( Float ) V. getheight () 47 /(View) v. getparent (). getheight (), V. getx () + V. getwidth () 48 /2, V. Gety () + V. getheight ()/2 ); 49 SA. setduration (2000 ); 50 Alphaanimation AA = New Alphaanimation (1f, 0f ); 51 AA. setduration (2000 ); 52 Animset. addanimation (SA ); 53 Animset. addanimation (AA ); 54 Mview. startanimation (animset ); 55 Mview. setvisibility (view. Gone ); 56 } 57 }
One thing to note: (float) v. getwidth ()/(View) v. getparent (). getwidth ()
- Both v. getwidth () and (View) v. getparent (). getwidth () are of the int type. To prevent the expected result from being 0, float is used to convert the numerator.
- V. getwidth () in the pop-up animation, it indicates the size of the image when the animation starts. In the hidden animation, it indicates the size of the image when the animation ends.
- (View) v. getparent ()). in the pop-up animation, getwidth () indicates the size of the view at the animation end. In the hidden animation, it indicates the size of the view at the animation start.
- The result of this ratio is the ratio of the width before and after the animation changes.