Android圖片放大縮小動(dòng)畫(huà),竟如此簡(jiǎn)單
有這樣一個(gè)需求,需要點(diǎn)擊圖片放大縮小動(dòng)畫(huà),效果:
我們借助Android自帶動(dòng)畫(huà)Animation ,很容易實(shí)現(xiàn)
初始化對(duì)象
- Animation animation;
- private ImageView iv_good;
- animation= AnimationUtils.loadAnimation(this, R.anim.anim_small);
按鈕點(diǎn)擊事件
- iv_good.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- iv_good.startAnimation(animation);
- }
- });
屬性動(dòng)畫(huà)
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"/>
下面我們重點(diǎn)來(lái)關(guān)注AnimationUtils 這個(gè)類中l(wèi)oadAnimation的方法,跟進(jìn)進(jìn)去看看
- /**
- * 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();
- }
- }
我們發(fā)現(xiàn)重要的是調(diào)用createAnimationFromXml方法。再次跟進(jìn)看看createAnimationFromXml方法。
- 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;
- }
細(xì)心的你,不難發(fā)現(xiàn)XmlPullParser,其實(shí)就是我們上面定義的anim_small.xml,解析出這份xml里面的屬性,進(jìn)行加載動(dòng)畫(huà)效果。Android系統(tǒng)已經(jīng)為我們解析分裝好,我們只需要使用輪子就好了。
- /**
- * 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;
- }
分享這個(gè)小例子的初衷,是希望大家對(duì)于一個(gè)小小的知識(shí)點(diǎn),我們可以跟進(jìn)看看其中的實(shí)現(xiàn)過(guò)程,了解過(guò)程,麻雀雖小但五臟俱全,希望對(duì)你有幫助。
【本文為51CTO專欄作者“洪生鵬”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)聯(lián)系原作者】