偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Android圖片放大縮小動(dòng)畫(huà),竟如此簡(jiǎn)單

移動(dòng)開(kāi)發(fā) Android
Android圖片放大縮小動(dòng)畫(huà),竟如此簡(jiǎn)單。分享這個(gè)小例子的初衷,是希望大家對(duì)于一個(gè)小小的知識(shí)點(diǎn),我們可以跟進(jìn)看看其中的實(shí)現(xiàn)過(guò)程,了解過(guò)程,麻雀雖小但五臟俱全,希望對(duì)你有幫助。

[[190226]]

有這樣一個(gè)需求,需要點(diǎn)擊圖片放大縮小動(dòng)畫(huà),效果:

我們借助Android自帶動(dòng)畫(huà)Animation ,很容易實(shí)現(xiàn)

初始化對(duì)象

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

按鈕點(diǎn)擊事件

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

屬性動(dòng)畫(huà)

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"/> 

下面我們重點(diǎn)來(lái)關(guān)注AnimationUtils 這個(gè)類中l(wèi)oadAnimation的方法,跟進(jìn)進(jìn)去看看

  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.    } 

我們發(fā)現(xiàn)重要的是調(diào)用createAnimationFromXml方法。再次跟進(jìn)看看createAnimationFromXml方法。

  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.     } 

細(xì)心的你,不難發(fā)現(xiàn)XmlPullParser,其實(shí)就是我們上面定義的anim_small.xml,解析出這份xml里面的屬性,進(jìn)行加載動(dòng)畫(huà)效果。Android系統(tǒng)已經(jīng)為我們解析分裝好,我們只需要使用輪子就好了。

  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.    } 

分享這個(gè)小例子的初衷,是希望大家對(duì)于一個(gè)小小的知識(shí)點(diǎn),我們可以跟進(jìn)看看其中的實(shí)現(xiàn)過(guò)程,了解過(guò)程,麻雀雖小但五臟俱全,希望對(duì)你有幫助。

【本文為51CTO專欄作者“洪生鵬”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)聯(lián)系原作者】

戳這里,看該作者更多好文

責(zé)任編輯:武曉燕 來(lái)源: 51CTO專欄
相關(guān)推薦

2018-04-11 10:07:09

大數(shù)據(jù)

2021-02-19 11:55:36

C語(yǔ)言MD5加密

2019-10-11 09:39:44

HTTP調(diào)用系統(tǒng)

2013-05-14 11:13:02

AIR Android放大縮小手勢(shì)

2017-12-15 16:03:28

2011-05-30 13:23:11

Android 動(dòng)畫(huà)

2019-07-31 14:34:00

數(shù)據(jù)庫(kù)MySQLJava

2015-07-06 10:52:19

BAT數(shù)據(jù)中心

2017-12-25 09:30:00

互聯(lián)網(wǎng)視頻流量耗費(fèi)成本

2021-07-05 06:51:41

Nacos微服務(wù)源碼

2021-04-23 16:40:49

Three.js前端代碼

2019-10-15 09:46:46

機(jī)器學(xué)習(xí)人工智能計(jì)算機(jī)

2020-02-20 16:07:45

IT需求

2015-12-07 10:00:13

HTML5Loading動(dòng)畫(huà)

2017-09-12 17:05:02

AndroidLoading客戶端

2011-08-19 17:02:46

iPhone開(kāi)發(fā)

2025-03-04 00:02:00

Python序列報(bào)錯(cuò)

2011-01-06 09:38:10

2016-12-26 18:16:41

戴爾成就篇

2020-12-28 07:47:35

動(dòng)態(tài)代理AOP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)