Android custom view tutorial 03 --- Android property animation details

Source: Internet
Author: User

Android custom view tutorial 03 --- Android property animation details
1 package com. example. donghuatest; 2 3 import android. animation. objectAnimator; 4 import android. animation. propertyValuesHolder; 5 import android. animation. valueAnimator; 6 import android. animation. valueAnimator. animatorUpdateListener; 7 import android. app. activity; 8 import android. OS. bundle; 9 import android. view. view; 10 import android. widget. imageView; 11 12 public class MainActivity extends Acti Detail {13 14 // relatively simple property animation 15 private ObjectAnimator alphaAnimation = new ObjectAnimator (); 16 17 private ImageView imageView; 18 19 @ Override20 protected void onCreate (Bundle savedInstanceState) {21 super. onCreate (savedInstanceState); 22 setContentView (R. layout. activity_main); 23 24 imageView = (ImageView) this. findViewById (R. id. iv); 25 imageView. setOnClickListener (new View. onClickListener () {26 27 @ Over Ride28 public void onClick (final View v) {29 rotateyAnimRunFirst (v); 30 // rotateyAnimRunSecond (v); 31 // rotateyAnimRunThird (v); 32} 33 }); 34 35} 36 37/** 38 * landscape flip Y axis unchanged 39*40 * @ param view41 */42 public void rotateyAnimRunFirst (final View view) {43 ObjectAnimator anim = ObjectAnimator. ofFloat (view, "rotationY", 0.0F, 44 360.0F ). setDuration (500); 45 anim. start (); 46} 47 48/** 49 * make the image larger and smaller, and the transparency also changes. Note that when the transparency changes to 0, it ends and does not return to the initial state 50*51 * @ param view52 */53 public void rotateyAnimRunSecond (final View view) {54 ObjectAnimator anim = ObjectAnimator. ofFloat (view, "zhy", 1.0F, 0.0F) 55. setDuration (500); 56 anim. start (); 57 anim. addUpdateListener (new AnimatorUpdateListener () {58 @ Override59 public void onAnimationUpdate (ValueAnimator animation) {60 float cVal = (Float) animation. getAnimatedValue (); 61 View. setAlpha (cVal); 62 view. setScaleX (cVal); 63 view. setScaleY (cVal); 64} 65 }); 66 67} 68 69/** 70 * Another method that contains multiple effects is more convenient than the second method. The final result is still the initialization Result 71*72 *@ param view73 */74 public void rotateyAnimRunThird (final View view) {75 PropertyValuesHolder pvhX = PropertyValuesHolder. ofFloat ("alpha", 1f, 76 0f, 1f); 77 PropertyValuesHolder pvhY = PropertyValuesHolder. ofFloat ("scaleX", 1f, 78 0, 1f); 79 Proper TyValuesHolder pvhZ = PropertyValuesHolder. ofFloat ("scaleY", 1f, 80 0, 1f); 81 ObjectAnimator. ofPropertyValuesHolder (view, pvhX, pvhY, pvhZ) 82. setDuration (1000 ). start (); 83 84} 85 86}: this is a small demo of the property animation. You can run it and have three effects in total. RotateyAnimRunFirst is actually a flip function. The rotateyAnimRunSecond function changes the image size and transparency. This is equivalent to a mix of animation effects. The rotateyAnimRunThird function has almost the same effect as the second function. However, this function will return to the initial effect at the end of the day, and the methods used are different. In fact, this property animation is also very easy to understand. Compared with the other two types of animation, the property animation changes the view itself. In the rotateyAnimRunFirst function, you can see that the value of rotationY has been passed in. Many people will ask what the value of rotationY is to be passed here. In fact, this place is quite understandable. For the method in which the first function is called, you can change the value of this attribute animation, provided that the get set Method of this value is required. If the value you pass does not have the get set method, the property animation will certainly not work. For example, the code here is the changed imageview. We can see that imageview inherits from the view, and the view contains 1 float mRotationY = 0f of these get set methods; 2 3/** 4 * The degrees rotation around the horizontal axis through the specified point. 5 */6 @ ViewDebug. exportedProperty 7 float mRotationX = 0f; 8 9/** 10 * The degrees rotation around the specified point.11 */12 @ ViewDebug. exportedProperty13 float mRotation = 0f; 14 15/*** 16 * The amount of translation The object away from its left property (post-layout ). 17 */18 @ ViewDebug. exportedProperty19 float mTranslationX = 0f; 20 21/**/*** The degrees that the view is rotated around the vertical axis through the destination point. ** @ see # getequaltx () * @ see # getequalty () * @ see # setRotationY (float) ** @ return The degrees of Y rotation. */public float getRotationY () {return mTransformationInfo! = Null? MTransformationInfo. mRotationY: 0;} So our first function can certainly be successfully executed. Then let's look at the second function. Anim. addUpdateListener (new AnimatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {float cVal = (Float) animation. getAnimatedValue (); view. setAlpha (cVal); view. setScaleX (cVal); view. setScaleY (cVal) ;}}); let's take a look at this and we can guess it. For the second type of function property animation calling, we can control it by ourselves. Float cVal = (Float) animation. getAnimatedValue (); view. setAlpha (cVal); view. setScaleX (cVal); view. setScaleY (cVal); after obtaining the value, you can change the attributes of v ~~~ It is easy to understand. So the code here can also be written in ObjectAnimator anim = ObjectAnimator. ofFloat (view, "tttt", 1.0F, 0.0F ). setDuration (500); The tttt can also be changed to the name you want to change without affecting the result, because we finally manually use the callback function to change the view attributes, this process is written by ourselves, so the name can be written here, while the first function cannot be called by the system itself through reflection, therefore, pay attention to ~~ The last function third is actually to show the process self-implemented in the system. We can trace the function/*** Constructs and returns an ObjectAnimator that animates between in the source code ObjectAnimator. float values. A single * value implies that value is the one being animated. two values imply a starting * and ending values. more than two values imply a starting value, values to animate through * along the way, and an ending value (these values w Ill be distributed evenly initialize SS * the duration of the animation ). ** @ param target The object whose property is to be animated. this object shoshould * have a public method on it called <code> setName () </code>, where <code> name </code> is * the value of the <code> propertyName </code> parameter. * @ param propertyName The name of the property being animated. * @ param values A set of values that the anim Ation will animate between over time. * @ return An ObjectAnimator object that is set up to animate between the given values. */public static ObjectAnimator ofFloat (Object target, String propertyName, float... values) {ObjectAnimator anim = new ObjectAnimator (target, propertyName); anim. setFloatValues (values); return anim. /*** Sets the values, per property, being animated. this function is called internally * by the constructors of ValueAnimator that take a list of values. but an ValueAnimator can * be constructed without values and this method can be called to set the values manually * instead. ** @ param values The set of values, per property, being animated. */public void setValues (PropertyValu EsHolder... values) {int numValues = values. length; mValues = values; mValuesMap = new HashMap <String, PropertyValuesHolder> (numValues); for (int I = 0; I <numValues; ++ I) {PropertyValuesHolder valuesHolder = (PropertyValuesHolder) values [I]; mValuesMap. put (valuesHolder. getPropertyName (), valuesHolder);} // New property/values/target shocould cause re-initialization prior to starting mInitialize D = false ;}@ Override public void setFloatValues (float... values) {if (mValues = null | mValues. length = 0) {// No values yet-this animator is being constructed piecemeal. init the values with // whatever the current propertyName is if (mProperty! = Null) {setValues (PropertyValuesHolder. ofFloat (mProperty, values);} else {setValues (PropertyValuesHolder. ofFloat (mPropertyName, values) ;}} else {super. setFloatValues (values );}} the method ofPropertyValuesHolder/*** Constructs and returns an ObjectAnimator that animates between the sets of values specified * in <code> PropertyValueHolder </code> objects. this variant shocould be use D when animating * several properties at once with the same ObjectAnimator, since PropertyValuesHolder allows * you to associate a set of animation values with a property name. ** @ param target The object whose property is to be animated. depending on how the * PropertyValuesObjects were constructed, the target object shoshould either have the {@ link * android. util. property} objects used to construc T the PropertyValuesHolder objects or (if the * PropertyValuesHOlder objects were created with property names) the target object shocould have * public methods on it called <code> setName () </code>, where <code> name </code> is the name of * the property passed in as the <code> propertyName </code> parameter for each of the * PropertyValuesHolder objects. * @ param values A set of PropertyValuesHolder obje Cts whose values will be animated between * over time. * @ return An ObjectAnimator object that is set up to animate between the given values. */public static ObjectAnimator ofPropertyValuesHolder (Object target, PropertyValuesHolder... values) {ObjectAnimator anim = new ObjectAnimator (); anim. mTarget = target; anim. setValues (values); return anim; this method is used in the end.

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.