1. 程式人生 > >和ys一起學動畫——傳統動畫與屬性動畫(一)

和ys一起學動畫——傳統動畫與屬性動畫(一)

最近閒啊,所以一直都在看些部落格。想到自己沒怎麼用過動畫,總不能老用別人寫好的動畫就滿足了吧。所以自己學習了幾天的動畫。

傳統動畫

傳統動畫,就是Android3.0之前就可以使用的 API 現在用的情況已經比較少了。但仍然還是學習一下,幀動畫(Frame by frame)偶爾還是會被用到。

1.補間動畫(Tweened Animations)

補間動畫主要有四種:
縮放(ScaleAnimation)
漸變(AlphaAnimation)
平移(TranslateAnimation)
旋轉(RotateAnimation)
Tweened動畫效果圖

方法1:
在程式碼中新增一個旋轉動畫:

    AnimationSet animationSet = new AnimationSet(true);
    //引數1 開始角度
    //引數2 結束角度
    //引數3、4 x軸位置
    //引數5、6 y軸位置
    RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setDuration(1000
);//設定動畫長度 單位毫秒 animationSet.addAnimation(rotateAnimation); image.startAnimation(animationSet);

方法2:
在資原始檔(/res/anim/xxx.xml)中設定一個透明度漸變動畫:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

   <alpha
       android:duration="500"
       android:fromAlpha
="1.0" android:startOffset="500" android:toAlpha="0.0" />
</set>

在程式碼中呼叫這個動畫:

    Animation animation = AnimationUtils.loadAnimation(this, R.anim.xxx);
    image.startAnimation(animation);

簡單講的話,Tween動畫就這麼多了。
但是,其實這個可以研究、可自定義的東西遠不止這些:
1.動畫可以設定插值器(interpolator),所謂插值器就是控制變化快慢的函式,比如你想要一個勻速的變換或者一個均勻加速又或者先快後慢等效果,可以直接呼叫系統中的插值器幫你完成。

animationSet.setInterpolator(new AccelerateDecelerateInterpolator());

系統自帶插值器
AccelerateDecelerateInterpolator 開始與結束的地方速率改變比較慢,在中間的時候加速

AccelerateInterpolator 開始的地方速率改變比較慢,然後開始加速

AnticipateInterpolator 開始的時候向後然後向前甩

AnticipateOvershootInterpolator 開始的時候向後然後向前甩一定值後返回最後的值

BounceInterpolator 動畫結束的時候彈起

CycleInterpolator 迴圈播放特定的次數,速率改變沿著正弦曲線

DecelerateInterpolator 在開始的地方快然後慢
建立的時候,可以傳factor值,如DecelerateInterpolator(2f):

LinearInterpolator 以常量速率改變(勻速)

OvershootInterpolator 向前甩一定值後再回到原來位置
建立的時候,可以傳tension值,OvershootInterpolator(0.8f):

2.動畫有一些其他的方法,比如
設定啟動延時: rotateAnimation1.setStartOffset(1000);

設定監聽器:

rotateAnimation1.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                    //動畫開始時觸發
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                    //動畫重複時觸發
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                    //動畫結束時觸發
                }
            });

設定迴圈重複:

anm.setRepeatMode(Animation.RESTART);
anm.setRepeatCount(Animation.INFINITE);

3.還有一個 你可以給你的Layout加上子控制元件新增進來時產生的動畫(LayoutAnimationsController):
LayoutAnimationsController可以用於實現使多個控制元件按順序一個一個的顯示。
LayoutAnimationsController效果圖

1)LayoutAnimationsController用於為一個layout裡面的控制元件,或者是一個ViewGroup裡面的控制元件設定統一的動畫效果。
2)每一個控制元件都有相同的動畫效果。
3)控制元件的動畫效果可以在不同的時間顯示出來。
4)LayoutAnimationsController可以在xml檔案當中設定,也可以在程式碼當中進行設定。

layout_anim.xml:

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_anim"
    android:animationOrder="normal"
    android:delay="0.5" />

item_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true" >

    <alpha
        android:duration="1500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>

layout設定:

    <LinearLayout
        android:layoutAnimation="@anim/layout_anim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/rotateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋轉" />

        <Button
            android:id="@+id/scaleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="縮放" />

        <Button
            android:id="@+id/alphaButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="淡入淡出" />

        <Button
            android:id="@+id/translateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="移動" />

        <Button
            android:id="@+id/nextDemoButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="next" />
    </LinearLayout>

就可以看到效果了。

當然這些同樣可以在程式碼裡建立和設定,這裡就不在多贅述了。
(建立Animation,建立LayoutAnimationController並對其進行設定,再將其設定給Layout物件)

2.幀動畫(Frame by Frame)

幀動畫我自己在Demo裡沒有做,沒有合適的素材哈哈。

android:oneshot 是否迴圈播放
android:drawable    Drawable資源,用於這一幀的圖片。
android:duration    Integer型別.該幀的時長,單位為毫秒milliseconds.

res/anim/xxx.xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

呼叫程式碼:

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.xxx);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();

屬性動畫

屬性動畫是近年來使用較多的做法,它直接修改操作物件的某一個屬性而不僅僅是對其進行影象上的變換。
就比如說 如果對一個按鈕進行補間動畫的縮放,那麼按鈕上的文字內容會被拉伸/縮小 得變形甚至難以辨認,
而屬性動畫的原理是修改這個按鈕的width和height屬性,所以不需要擔心文字會被變化得變形。

由於篇幅已經挺長了,關於更多的屬性動畫內容我們下篇文章再一起學習!

先放張我Demo目前的效果圖吧:

Demo效果圖
Demo會在下篇文章開放下載(也許還會修改 新增部分內容)

相關推薦

ys一起動畫——傳統動畫屬性動畫

最近閒啊,所以一直都在看些部落格。想到自己沒怎麼用過動畫,總不能老用別人寫好的動畫就滿足了吧。所以自己學習了幾天的動畫。 傳統動畫 傳統動畫,就是Android3.0之前就可以使用的 API 現在用的情況已經比較少了。但仍然還是學習一下,幀動畫(Fram

靜態連結庫的編譯使用 linux下的動態連結庫靜態連結庫到底是個什麼鬼?靜態連結庫的編譯使用

linux下的動態連結庫和靜態連結庫到底是個什麼鬼?(一)靜態連結庫的編譯與使用       知識不等於技術,這句話真的是越工作的時間長越深有體會,學習到的知識只有不斷的實踐,才成真正在自已的心裡紮下根,成為自身的一部分,所以無論如何,我希望我的部落格可以

從頭開始MySQL-------儲存過程儲存函式4

儲存過程與儲存函式的補充 MySQL的儲存過程與儲存函式有什麼區別?         儲存函式只能通過return語句返回單個值或者表物件。         儲存過程不能用return,但是可

S4 BP增強 BDT方式 新增自定義螢幕 KNA1KNVV 通用資料檢視、銷售分銷檢視英文

其他參考(中文):https://blog.csdn.net/guangcong2009/article/details/80569877 網上沒找到銷售與分銷 檢視:KNVV的新增方式,發出來分享下。 1、新增銷售區域螢幕時, 直接將下步驟KNA1改為 KNVV即可。 2、螢

動畫輕鬆理解時間複雜度

原文連結:看動畫輕鬆理解時間複雜度(一) 演算法(Algorithm)是指用來操作資料、解決程式問題的一組方法。對於同一個問題,使用不同的演算法,也許最終得到的結果是一樣的,比如排序就有前面的十大經典排序和幾種奇葩排序,雖然結果相同,但在過程中消耗的資源和時間卻會有很大的區別,比如快速排序與猴子排

大家一起python-day4-簡單的字串功能部分

# 1.字串的查詢 a = '123abc' # 1.1 print(a.count('2')) # 查詢到在1的位置 # 1.2 find從左邊開始查詢 print(a.find("3")) # 2 print(a.find('bb')) # 如果無法查詢到則返回-1 # 1.3 r

跟我一起Multiple View Geometry多檢視幾何5程式設計實踐課

  前言:博主今天把Multiple View Geometry第九章後半部分也讀完了,想著寫點對應的程式碼練練手,程式基本思路如下:讀入兩張有一定視差的圖片分別提取出ORB特徵點以及描述子,再對描述子進行暴力匹配篩選獲得若干goodmatches,然後呼叫Op

跟我一起Multiple View Geometry多檢視幾何2

前言:本篇部落格主要講fundamental matrix的由來與推導 9.2 The fundamental matrix F   綜合來說fundamental matrix就是epipolar geometry的代數表示,接下來我們從點和它對應的epi

從零開始C++之虛擬函式多型:虛擬函式表指標、虛解構函式、object slicing虛擬函式、C++物件模型圖

#include <iostream>using namespace std;class CObject {public:     virtual void Serialize()     {         cout << "CObject::Serialize ..." <&

【Android 動畫】View Animation詳解

安卓平臺目前提供了兩大類動畫,在Android 3.0之前,一大類是View Animation,包括Tween animation(補間動畫),Frame animation(幀動畫),在android3.0中又引入了一個新的動畫系統:property ani

《密碼編碼網路安全》原理實踐筆記

第一章: 安全服務有:同等實體i認證、資料來源認證、訪問控制、保密性、流量保密性、資料完整性、不可否認性、可用性 安全機制有:加密、資料簽名、訪問控制、資料完整性、認證交換、流量填充、路由控制、公證 關鍵術語:訪問控制、拒絕服務、被動威脅、主動威脅、加密、重播、認證、完整

計算幾何圖形有關的幾種常用演算法

我的專業是計算機輔助設計(CAD),算是一半機械一半軟體,《計算機圖形學》是必修課,也是我最喜歡的課程。熱衷於用程式碼擺平一切的我幾乎將這本教科書上的每種演算法都實現了一遍,這種重複勞動雖然意義不大,但是收穫很多,特別是丟棄了多年的數學又重新回到了腦袋中,算是最大的收

R語言 quantile()fivenum()的差別在於——加權平均算術平均轉載

轉自:http://blog.sina.com.cn/s/blog_a184ae810102xqwe.html quantile()和fivenum()的本質差別在於,quantile()函式的演算法是採用加權平均,fivenum()是算術平均。這麼說可能不易理解,其

【ML專案】基於網路爬蟲資料探勘演算法的web招聘資料分析——資料獲取處理

前言 這個專案是在學校做的,主要是想對各大招聘網站的招聘資料進行分析,沒準能從中發現什麼,這個專案週期有些長,以至於在專案快要結束時發現網上已經有了一些相關的專案,我後續會把相關的專案材料放在我的GitHub上面,連結為:https://github.com/

java內存管理GC機制

大於 一個棧 es2017 記錄 高速 工作 限制 fin 不存在 計算機cpu運轉速度越來越快,硬盤遠遠跟不上cpu的讀寫速度,就設計可內存。隨著cpu的發展,內存的讀寫速度也跟不上cpu處理速度,就在每顆cpu上加入了高速緩存。在多處理器系統中,每個處理

[神經網絡深度學習]使用神經網絡識別手寫數字

線性 部分 logs 結構 這一 可用 調整 重復 http 1.1 感知器 感知器的輸出為: wj為權重,表示相應輸入對輸出的重要性; threshold為閾值,決定神經元的輸出為0或1。 也可用下式表示: 其中b=-threshold,稱為感知器的偏置

用CocosCreatorPomelo編寫多人在線實時聊天室----基礎知識環境安裝

shu 以及 pan 信息 ast pre alt web 技術 客戶端:Cocos Creator 1.6.2服務器端:Pomelo 2.2.5源碼地址:https://github.com/foupwang/CocosCreatorChatForPomelo.git 本

Charles的基本操作方法python基礎

inpu 格式 format log text src 基礎 pos -s 開始與結束按鈕: 斷點按鈕: 清空按鈕: 若抓不到包可嘗試更改瀏覽器重新請求 python基礎(一) 計算機語言分為編譯型語言和解釋型語言,編譯型語言需要提前編譯,然後直接拿來運行,但解釋型語

進程線程=====>線程安全

locks 出錯 .get start string 異常 interrupt str 生成 1.並發訪問(同一個時間段內執行)<====>並行(同時刻)2.Thread.sleep(1000);//當前線程睡1秒(1000毫秒)當前線程休息,其他線程先占用資源

進程線程=====>進程

uil 檢查 procs pac errors das 初始 兩種 oss Java提供了兩種方法用來啟動進程或其它程序: (1)使用Runtime的exec()方法 (2)使用ProcessBuilder的start()方法 2.1.1 ProcessBuilder