Android image zooming animation is so simple

Android image zooming animation is so simple

[[190226]]

There is such a requirement, you need to click on the picture to zoom in and out animation, the effect is:

We can easily implement it with the help of Android's built-in animation

Initializing an object

  1. Animation animation;
  2. private ImageView iv_good;
  3. animation= AnimationUtils.loadAnimation(this, R.anim.anim_small);

Button click event

  1. iv_good.setOnClickListener(new View .OnClickListener() {
  2. @Override
  3. public void onClick( View   view ) {
  4. iv_good.startAnimation(animation);
  5. }
  6. });

Property Animation

res/anim/anim_small.xml

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. < set xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. android:fillAfter= "false" >
  4. <scale
  5. android:duration= "300"  
  6. android:fromXScale= "1"  
  7. android:fromYScale= "1"  
  8. android:pivotX= "50%"  
  9. android:pivotY= "50%"  
  10. android:toXScale= "2"  
  11. android:toYScale= "2" />
  12. <scale
  13. android:duration= "300"  
  14. android:fromXScale= "1"  
  15. android:fromYScale= "1"  
  16. android:pivotX= "50%"  
  17. android:pivotY= "50%"  
  18. android:startOffset= "300"  
  19. android:toXScale= "0.5"  
  20. android:toYScale= "0.5" />
  21. </ set >
  1. <ImageView
  2. android:id= "@+id/iv_good"  
  3. android:layout_width= "wrap_content"  
  4. android:layout_height= "wrap_content"  
  5. android:src= "@mipmap/ic_good" />

Next, let's focus on the loadAnimation method in the AnimationUtils class and follow it up.

  1. /**
  2. * Loads an {@link Animation} object from a resource
  3. *
  4. * @param context Application context used to access resources
  5. * @param id The resource id of the animation to   load  
  6. * @ return The animation object reference by the specified id
  7. * @throws NotFoundException when the animation cannot be loaded
  8. */
  9. public   static Animation loadAnimation(Context context, @AnimRes int id)
  10. throws NotFoundException {
  11. XmlResourceParser parser = null ;
  12. try {
  13. parser = context.getResources().getAnimation(id);
  14. return createAnimationFromXml(context, parser);
  15. } catch (XmlPullParserException ex) {
  16. NotFoundException rnf = new NotFoundException( "Can't load animation resource ID #0x" +
  17. Integer .toHexString(id));
  18. rnf.initCause(ex);
  19. throw rnf;
  20. } catch (IOException ex) {
  21. NotFoundException rnf = new NotFoundException( "Can't load animation resource ID #0x" +
  22. Integer .toHexString(id));
  23. rnf.initCause(ex);
  24. throw rnf;
  25. finally
  26. if (parser != null ) parser. close ();
  27. }
  28. }

We found that the important thing is to call the createAnimationFromXml method. Let's follow up again and look at the createAnimationFromXml method.

  1. private static Animation createAnimationFromXml(Context c, XmlPullParser parser)
  2. throws XmlPullParserException, IOException {
  3. return createAnimationFromXml(c, parser, null , Xml.asAttributeSet(parser));
  4. }
  1. private static Animation createAnimationFromXml(Context c, XmlPullParser parser,
  2. AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {
  3. Animation anim = null ;
  4. // Make sure we are on a start tag.
  5. int type;
  6. int depth = parser.getDepth();
  7. while (((type=parser. next ()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
  8. && type != XmlPullParser.END_DOCUMENT) {
  9. if (type != XmlPullParser.START_TAG) {
  10. continue ;
  11. }
  12. String name = parser.getName();
  13. if ( name .equals( "set" )) {
  14. anim = new AnimationSet(c, attrs);
  15. createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
  16. } else if ( name .equals( "alpha" )) {
  17. anim = new AlphaAnimation(c, attrs);
  18. } else if ( name .equals( "scale" )) {
  19. anim = new ScaleAnimation(c, attrs);
  20. } else if ( name .equals( "rotate" )) {
  21. anim = new RotateAnimation(c, attrs);
  22. } else if ( name .equals( "translate" )) {
  23. anim = new TranslateAnimation(c, attrs);
  24. } else {
  25. throw new RuntimeException( "Unknown animation name: " + parser.getName());
  26. }
  27. if (parent != null ) {
  28. parent.addAnimation(anim);
  29. }
  30. }
  31. return anim;
  32. }

If you are careful, you will find that XmlPullParser is actually the anim_small.xml we defined above, which parses the attributes in this xml to load the animation effect. The Android system has already parsed and packaged it for us, and we just need to use the wheel.

  1. /**
  2. * Add a child animation to this animation set .
  3. * The transforms of the child animations are applied in the order  
  4. * that they were added
  5. * @param a Animation to   add .
  6. */
  7. public void addAnimation(Animation a) {
  8. mAnimations.add (a);
  9. boolean noMatrix = (mFlags & PROPERTY_MORPH_MATRIX_MASK) == 0;
  10. if (noMatrix && a.willChangeTransformationMatrix()) {
  11. mFlags |= PROPERTY_MORPH_MATRIX_MASK;
  12. }
  13. boolean changeBounds = (mFlags & PROPERTY_CHANGE_BOUNDS_MASK) == 0;
  14. if (changeBounds && a.willChangeBounds()) {
  15. mFlags |= PROPERTY_CHANGE_BOUNDS_MASK;
  16. }
  17. if ((mFlags & PROPERTY_DURATION_MASK) == PROPERTY_DURATION_MASK) {
  18. mLastEnd = mStartOffset + mDuration;
  19. } else {
  20. if (mAnimations. size () == 1) {
  21. mDuration = a.getStartOffset() + a.getDuration();
  22. mLastEnd = mStartOffset + mDuration;
  23. } else {
  24. mLastEnd = Math. max (mLastEnd, a.getStartOffset() + a.getDuration());
  25. mDuration = mLastEnd - mStartOffset;
  26. }
  27. }
  28. mDirty = true ;
  29. }

The original intention of sharing this small example is to hope that everyone can follow up on the implementation process of a small knowledge point and understand the process. Although the sparrow is small, it has all the necessary functions. I hope it will be helpful to you.

[This article is an original article by 51CTO columnist "Hong Shengpeng". Please contact the original author for reprinting.]

Click here to read more articles by this author

<<:  Android takes you to analyze ScrollView-imitation QQ space title bar gradient

>>:  In-depth understanding of Android's rendering mechanism

Recommend

Tucao | Why is there such outrageous fake news?

You don’t know until you see it, and you’ll be sh...

Don't criticize Baidu, no one is innocent

[[162080]] Recently, the Baidu selling forum even...

How do these five industries seize the dividends of Zhihu's advertising channel?

Which industries and companies have already condu...

Google confesses to providing employee data to the FBI

On December 23, 2014, Christmas Eve, Google infor...

Is H5 really becoming popular?

A few days ago, I saw a friend in the Internet ci...

Why is your “old bring new” fission activity not working?

The global conversion rate of the project in this...

Methodology: Use the idea of ​​creating a "hot product" to operate content

When doing content operations , most people hope ...

Douyin traffic and fan increase methods and operation guide!

With the advent of the 5G era, short videos are s...

Inventory of Kuaishou advertising resources

1. Dual feed stream resource entry Note: The norm...