Android動畫(1):View動畫

View動畫:平移烁巫、縮放、旋轉(zhuǎn)磁餐、透明度;

對應的在xml中的標簽為:traslate阿弃、scale羞延、rotate脾还、alpha;

四種變換效果對應Animation的四個子類:TranslateAnimation、ScaleAnimation赛蔫、RotateAnimation泥张、AlphaAnimation.

描述view動畫的xml語法如下所示:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true"
    android:fillAfter="true">

    <translate android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:toXDelta="80%p"
        android:toYDelta="80%p"/>

    <scale android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="0.2"
        android:toYScale="0.2"
        />

    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        />

    <alpha android:fromAlpha="1"
        android:toAlpha="0.5"
        />

</set>

其中,<set>標簽標示動畫的集合渗钉,對應于AnimationSet類钞钙,其中interpalator屬性表示動畫所采用的插值器,默認為@android:anim/accelerate_decelerate_interpolator,先加速芒炼,后減速的效果本刽。shareInterpolator屬性表示集合中的動畫是否都共享同一個插值器。
對于xml中各種數(shù)值的單位子寓,比如80%指相對于自身的80%,該數(shù)值實際上代表了兩種屬性炸裆,一個pivotType和pivotValue,80%在java代碼中對應于pivotType = Animation.RELATIVE_TO_SELF,
pivot
Value = 0.8f鲜屏。另外一種是相當于父布局,比如80%p,此時听系,pivot*Type = Animation.RELATIVE_TO_PARENT虹菲。

Java代碼如下:

AnimationSet animationSet = new AnimationSet(true);
                ScaleAnimation scaleAnimation = new ScaleAnimation(0.1f,1f,0.1f,1f,
                        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT,0.5f,
                        Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT,0.5f);
                RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f,1f);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(rotateAnimation);
                animationSet.addAnimation(alphaAnimation);
                animationSet.addAnimation(translateAnimation);
                animationSet.setFillAfter(true);
                scaleAnimation.setDuration(3000);
                translateAnimation.setDuration(3000);
                alphaAnimation.setDuration(3000);
                rotateAnimation.setDuration(500);
                rotateAnimation.setRepeatCount(6);
                mShowAnimationTv.startAnimation(animationSet);

上面的代碼是將左上角的textView移到右下角,并且伴隨著旋轉(zhuǎn)浪漠,透明度,縮放的動畫(代碼實現(xiàn)時要注意set集合add時的順序)该镣。

實現(xiàn)原理

首先响谓,從動畫開始的地方AnimatonUtils.loadAnimaiton()方法開始,源碼如下:

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();
        }
    }

最后會調(diào)用createAnimationFromXml(context, parser),此處使用了XmlResourceParse嫁审。

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;

    }

最終會調(diào)用XmlPullParser,基于事件驅(qū)動模型來解析xml文件中的動畫律适,從代碼中可以看出來遏插,每一個節(jié)點都對應一個相應的View動畫類,這樣與實際寫代碼差不多厂僧,在Java代碼中也是先創(chuàng)建出相應的view動畫類胎围,然后按照順序添加到集合中。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市上岗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌敬锐,老刑警劉巖呆瞻,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件痴脾,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機冤灾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門辕近,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人归粉,你說我怎么就攤上這事漏峰。” “怎么了芽狗?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵童擎,是天一觀的道長。 經(jīng)常有香客問我顾复,道長,這世上最難降的妖魔是什么萧芙? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任假丧,我火速辦了婚禮,結(jié)果婚禮上渔期,老公的妹妹穿的比我還像新娘渴邦。我一直安慰自己,他們只是感情好谋梭,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布瓮床。 她就那樣靜靜地躺著产镐,像睡著了一般矾策。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上贾虽,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天蓬豁,我揣著相機與錄音,去河邊找鬼地粪。 笑死,一個胖子當著我的面吹牛玩敏,可吹牛的內(nèi)容都是我干的质礼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼砰粹,長吁一口氣:“原來是場噩夢啊……” “哼造挽!你這毒婦竟也來了碱璃?” 一聲冷哼從身側(cè)響起饭入,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤圣拄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后庇谆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凭疮,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡执解,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年纲酗,在試婚紗的時候發(fā)現(xiàn)自己被綠了新蟆。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡吮螺,死狀恐怖帕翻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情紫岩,我是刑警寧澤睬塌,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站勋陪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏粥鞋。R本人自食惡果不足惜瞄崇,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望等浊。 院中可真熱鬧摹蘑,春花似錦、人聲如沸衅鹿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽泵三。三九已至衔掸,卻和暖如春俺抽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背磷斧。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工瞳抓, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留埃疫,地道東北人孩哑。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓横蜒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親丛晌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容