[[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 - Animation animation;
- private ImageView iv_good;
- animation= AnimationUtils.loadAnimation(this, R.anim.anim_small);
Button click event - iv_good.setOnClickListener(new View .OnClickListener() {
- @Override
- public void onClick( View view ) {
- iv_good.startAnimation(animation);
- }
- });
Property Animation res/anim/anim_small.xml - <?xml version= "1.0" encoding= "utf-8" ?>
- < set xmlns:android= "http://schemas.android.com/apk/res/android"
- android:fillAfter= "false" >
- <scale
- android:duration= "300"
- android:fromXScale= "1"
- android:fromYScale= "1"
- android:pivotX= "50%"
- android:pivotY= "50%"
- android:toXScale= "2"
- android:toYScale= "2" />
- <scale
- android:duration= "300"
- android:fromXScale= "1"
- android:fromYScale= "1"
- android:pivotX= "50%"
- android:pivotY= "50%"
- android:startOffset= "300"
- android:toXScale= "0.5"
- android:toYScale= "0.5" />
- </ set >
- <ImageView
- android:id= "@+id/iv_good"
- android:layout_width= "wrap_content"
- android:layout_height= "wrap_content"
- android:src= "@mipmap/ic_good" />
Next, let's focus on the loadAnimation method in the AnimationUtils class and follow it up. - /**
- * Loads an {@link Animation} object from a resource
- *
- * @param context Application context used to access resources
- * @param id The resource id of the animation to load
- * @ return The animation object reference by the specified id
- * @throws NotFoundException when the animation cannot be loaded
- */
- public static Animation loadAnimation(Context context, @AnimRes int id)
- throws NotFoundException {
- XmlResourceParser parser = null ;
- try {
- parser = context.getResources().getAnimation(id);
- return createAnimationFromXml(context, parser);
- } catch (XmlPullParserException ex) {
- NotFoundException rnf = new NotFoundException( "Can't load animation resource ID #0x" +
- Integer .toHexString(id));
- rnf.initCause(ex);
- throw rnf;
- } catch (IOException ex) {
- NotFoundException rnf = new NotFoundException( "Can't load animation resource ID #0x" +
- Integer .toHexString(id));
- rnf.initCause(ex);
- throw rnf;
- finally
- if (parser != null ) parser. close ();
- }
- }
We found that the important thing is to call the createAnimationFromXml method. Let's follow up again and look at the createAnimationFromXml method. - private static Animation createAnimationFromXml(Context c, XmlPullParser parser)
- throws XmlPullParserException, IOException {
- return createAnimationFromXml(c, parser, null , Xml.asAttributeSet(parser));
- }
- private static Animation createAnimationFromXml(Context c, XmlPullParser parser,
- AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {
- Animation anim = null ;
- // Make sure we are on a start tag.
- int type;
- int depth = parser.getDepth();
- while (((type=parser. next ()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
- && type != XmlPullParser.END_DOCUMENT) {
- if (type != XmlPullParser.START_TAG) {
- continue ;
- }
- String name = parser.getName();
- if ( name .equals( "set" )) {
- anim = new AnimationSet(c, attrs);
- createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
- } else if ( name .equals( "alpha" )) {
- anim = new AlphaAnimation(c, attrs);
- } else if ( name .equals( "scale" )) {
- anim = new ScaleAnimation(c, attrs);
- } else if ( name .equals( "rotate" )) {
- anim = new RotateAnimation(c, attrs);
- } else if ( name .equals( "translate" )) {
- anim = new TranslateAnimation(c, attrs);
- } else {
- throw new RuntimeException( "Unknown animation name: " + parser.getName());
- }
- if (parent != null ) {
- parent.addAnimation(anim);
- }
- }
- return anim;
- }
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. - /**
- * Add a child animation to this animation set .
- * The transforms of the child animations are applied in the order
- * that they were added
- * @param a Animation to add .
- */
- public void addAnimation(Animation a) {
- mAnimations.add (a);
- boolean noMatrix = (mFlags & PROPERTY_MORPH_MATRIX_MASK) == 0;
- if (noMatrix && a.willChangeTransformationMatrix()) {
- mFlags |= PROPERTY_MORPH_MATRIX_MASK;
- }
- boolean changeBounds = (mFlags & PROPERTY_CHANGE_BOUNDS_MASK) == 0;
- if (changeBounds && a.willChangeBounds()) {
- mFlags |= PROPERTY_CHANGE_BOUNDS_MASK;
- }
- if ((mFlags & PROPERTY_DURATION_MASK) == PROPERTY_DURATION_MASK) {
- mLastEnd = mStartOffset + mDuration;
- } else {
- if (mAnimations. size () == 1) {
- mDuration = a.getStartOffset() + a.getDuration();
- mLastEnd = mStartOffset + mDuration;
- } else {
- mLastEnd = Math. max (mLastEnd, a.getStartOffset() + a.getDuration());
- mDuration = mLastEnd - mStartOffset;
- }
- }
- mDirty = true ;
- }
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 |