android anim 動畫詳細(xì)介紹.doc_第1頁
android anim 動畫詳細(xì)介紹.doc_第2頁
android anim 動畫詳細(xì)介紹.doc_第3頁
android anim 動畫詳細(xì)介紹.doc_第4頁
android anim 動畫詳細(xì)介紹.doc_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

Android動畫及圖片的縮放和旋轉(zhuǎn) Android動畫有2種,一種是Tween Animation,另一種是Frame Animation,先說說Tween動畫吧。Tween動畫是對視圖對象中的內(nèi)容進(jìn)行一系列簡單的轉(zhuǎn)換,比如位置的移動,大小的縮放,旋轉(zhuǎn),透明度得變化等等。Tween動畫可以寫到一個(gè)xml文件中,就像定義布局文件一樣,當(dāng)然,也可以寫到android代碼中,不過推薦寫到xml文件中,因?yàn)樗邆涞拈喿x性,可重用性大大超過了硬編碼。xml文件放在工程的res/anim目錄中,這個(gè)目錄中要包含一個(gè)根元素,可以是,或者,當(dāng)然,這些元素都可以放到一個(gè)動畫集合中,默認(rèn)情況下,所有的動畫指令都是同時(shí)發(fā)生的,為了讓他們按順序發(fā)生,需要設(shè)置一個(gè)特殊的屬性startOffset。下面定義了一個(gè)動畫的集合:1 2 4 13 14 15 25 33 34 這里解釋一下這段代碼的作用:首先是一個(gè)動畫的集合set,在這個(gè)set中有一個(gè)屬性shareInterpolater,如果設(shè)置為true,則這個(gè)集合下面所有的子元素都享有同樣的interpolater,api上面是這樣說的:android:shareInterpolator Boolean. true if you want to share the same interpolator among all child elements. 緊跟在這個(gè)集合后面的是一個(gè)縮放動畫,里面有一些個(gè)屬性,下面一一介紹: android:interpolator屬性:這是值定一個(gè)動畫的插入器,有一些常用的插入器:accelerate_decelerate_interpolator加速-減速動畫插入器,顧名思義,就是先加速后減速,accelerate_interpolator加速動畫插入器,decelerate_interpolator減速動畫插入器 android:fromXScale屬性為動畫起始時(shí),x坐標(biāo)上的伸縮尺寸 android:toXScal屬性為動畫結(jié)束時(shí),x坐標(biāo)上的伸縮尺寸 android:fromYScale屬性為動畫起始時(shí),y坐標(biāo)上的伸縮尺寸 android:toYScale屬性為動畫結(jié)束時(shí),y坐標(biāo)上的伸縮尺寸 關(guān)于伸縮尺寸這里還要羅嗦一下: 也就是上面的四個(gè)屬性的值:0.0表示收縮到?jīng)]有,1.0表示正常無收縮,值小于1.0表示收縮,值大于1.0表示放大。 android:fillAfter屬性當(dāng)設(shè)置為true時(shí),該動畫轉(zhuǎn)化在動畫結(jié)束后被應(yīng)用,同理還有android:fillBefore屬性,當(dāng)設(shè)置為true時(shí),該動畫轉(zhuǎn)化在動畫開始前被應(yīng)用 android:duration屬性表示動畫持續(xù)的時(shí)間,單為時(shí)毫秒 android:pivotX屬性為動畫相對于x坐標(biāo)的起始位置 android:pivotY屬性為動畫相對于y坐標(biāo)的起始位置 這2個(gè)屬性有不同的格式表示,如值為50,表示是相對于父類的50%,如值為50%,表示是相對于自己的50% 這里的50%表示相對于自身x,y坐標(biāo)上的中點(diǎn)位置 緊跟著是一個(gè)動畫集合,里面有縮放和旋轉(zhuǎn),這個(gè)集合的interpolater為減速動畫插入器 這里的縮放里面還有一個(gè)屬性,android:startOffset屬性是設(shè)置動畫開始的時(shí)間,這里設(shè)置700是表示700毫秒之后開始,也就是第一個(gè)縮放完成之后開始。 旋轉(zhuǎn)里面的屬性跟scale里面的都差不多,只是旋轉(zhuǎn)講究的角度。 android:fromDegrees屬性表示動畫起始時(shí)的角度 android:toDegrees屬性表示動畫結(jié)束時(shí)旋轉(zhuǎn)的角度,可以大于360度 動畫文件寫好了之后,我們就可以在代碼中調(diào)用這個(gè)動畫了,先寫一個(gè)布局文件,布局文件里面有一個(gè)ImageView,然后我們讓這個(gè)ImageView做動畫。 布局文件如下: 1 2 6 11 然后我們讓這個(gè)圖片按照我們xml中指定的動畫運(yùn)動: 代碼: package com.test.shang;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.TransitionDrawable;import android.os.Bundle;import android.view.MotionEvent;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.AnimationUtils;import android.widget.ImageView;public class TestStyle extends Activity AnimationDrawable animationDrawable;Overrideprotected void onCreate (Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.test);ImageView iv = (ImageView) findViewById(R.id.imageView1);Animation animation = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set);iv.setAnimation(animation);animation.start();因?yàn)檫@里是動畫,不好截圖,所以我就不截圖了,具體效果大家可以試試看。 下面接著說說Frame Animation吧:也就是幀動畫,可以使用AndroidDrawable來負(fù)責(zé)幀動畫,同樣它可以在xml文件中很方便的列出所有的幀,按照周期去執(zhí)行每幀動畫,下面是一個(gè)定義幀動畫的例子: 1 2 4 5 6 7 這里定義了幀的名字和每幀的持續(xù)時(shí)間. 這里有3幀,通過設(shè)置android:oneshot屬性為true,它將會在最后一幀停下來,如果設(shè)置為false,它將會循環(huán)播放,可以把它添加到一個(gè)背景中,讓他播放,具體代碼如下: 布局文件: 1 2 6 10 在代碼里面設(shè)置imageview的背景圖片,然后做動畫: 1 package com.test.shang;2 3 import android.app.Activity;4 import android.graphics.drawable.AnimationDrawable;5 import android.graphics.drawable.TransitionDrawable;6 import android.os.Bundle;7 import android.view.MotionEvent;8 import android.view.animation.Animation;9 import android.view.animation.AnimationSet;10 import android.view.animation.AnimationUtils;11 import android.widget.ImageView;12 13 public class TestStyle extends Activity 14 AnimationDrawable animationDrawable;15 16 Override17 protected void onCreate (Bundle savedInstanceState) 18 super.onCreate(savedInstanceState);19 setContentView(R.layout.test);20 ImageView iv = (ImageView) findViewById(R.id.imageView1);21 iv.setBackgroundResource(R.anim.anim_list);22 animationDrawable = (AnimationDrawable) iv.getBackground();23 24 Override25 public boolean onTouchEvent (MotionEvent event) 26 if(event.getAction() = MotionEvent.ACTION_DOWN) 27 animationDrawable.start();28 return true;29 30 return super.onTouchEvent(event);31 32 這里需要注意的是:AnimationDrawable在調(diào)用OnCreate的過程中不能調(diào)用start(),這是因?yàn)锳nimationDrawable不能在不完全的窗口上運(yùn)行,需要一個(gè)操作來觸發(fā),如果你想立即播放動畫,沒有必要的交互,你可以在onWindowFocusChanged()方法中調(diào)用它,這樣它將會成為窗口焦點(diǎn)。 下面說說圖片的縮放和旋轉(zhuǎn): 這里我就寫的比較簡單了,代碼里面的注釋很詳細(xì),可以慢慢看。 1 package com.test.shang;2 3 import android.app.Activity;4 import android.graphics.Bitmap;5 import android.graphics.BitmapFactory;6 import android.graphics.Matrix;7 import android.graphics.drawable.BitmapDrawable;8 import android.os.Bundle;9 import android.widget.ImageView;10 import android.widget.ImageView.ScaleType;11 import android.widget.LinearLayout;12 import android.widget.LinearLayout.LayoutParams;13 14 public class BitmapTest extends Activity 15 16 Override17 protected void onCreate (Bundle savedInstanceState) 18 super.onCreate(savedInstanceState);19 setTitle(測試圖片的縮放和旋轉(zhuǎn));20 LinearLayout layout = new LinearLayout(this);21 22 /加載需要操作的圖片,這里是機(jī)器貓的圖片23 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.duola);24 25 /獲取這個(gè)圖片的寬和高26 int width = bitmapOrg.getWidth();27 int height = bitmapOrg.getHeight();28 29 /定義預(yù)轉(zhuǎn)換成的圖片的寬和高30 int newWidth = 200;31 int newHight = 200;32 33 /計(jì)算縮放率,新尺寸除原尺寸34 float scaleWidth = (float)newWidth/width;35 float scaleHeight = (float)newHight/height;36 37 /創(chuàng)建操作圖片用的matrix對象38 Matrix matrix = new Matrix();39 40 /縮放圖片動作41 matrix.postScale(scaleWidth, scaleHeight);42 43 /旋轉(zhuǎn)圖片動作44 matrix.postRotate(45);45 46 /創(chuàng)建新的圖片47 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);48 49 /將上面創(chuàng)建的Bitmap轉(zhuǎn)換成Drawable對象,使得其可以使用在imageView,imageButton上。50 BitmapDrawable bitmapDrawable = new BitmapDrawable(resizedBitmap);51 52 /創(chuàng)建一個(gè)ImageView53 ImageView iv = new ImageView(this);54 55 /將imageView的圖片設(shè)置為上面轉(zhuǎn)換的圖片56 iv.setImageDrawable(bitmapDrawable);57 58 /將圖片居中顯示59 iv.setScaleType(ScaleType.CENTER);60 61 /將imageView添加到布局模板中62 layout.addView(iv, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);63 64 /設(shè)置為本activity的模板65 setContentView(layout);66 67 下面是效果預(yù)覽: 好了,今天就到這里吧。本文主要研究Android中的三種動畫,第一種是單個(gè)View的各種動畫效果,第二種是兩個(gè)Activity切換時(shí)的動畫效果,第三種是類似于Gif的Frame By Frame動畫效果,其中View的各種動畫包括在xml文件中定義和代碼中定義兩種方式。一、動畫基本類型:如下表所示,Android的動畫由四種類型組成,即可在xml中定義,也可在代碼中定義,如下所示: XMLCODE 漸變透明度動畫效果alphaAlphaAnimation 漸變尺寸伸縮動畫效果scaleScaleAnimation 畫面轉(zhuǎn)換位置移動動畫效果translateTranslateAnimation 畫面轉(zhuǎn)移旋轉(zhuǎn)動畫效果rotateRotateAnimation二、如何在XML文件中定義動畫1.alpha 2.scale 3.translate4.rotate三、如何使用XML中定義的動畫 public static Animation loadAnimation (Contextcontext, int id)/第一個(gè)參數(shù)Context為程序的上下文 /第二個(gè)參數(shù)id為動畫XML文件的引用/例子:myAnimation=AnimationUtils.loadAnimation(this,R.anim.my_action);/使用AnimationUtils類的靜態(tài)方法loadAnimation()來加載XML中的動畫XML文件四、如何在java代碼中定義動畫1代碼:/在代碼中定義 動畫實(shí)例對象private Animation myAnimation_Alpha;private Animation myAnimation_Scale;private Animation myAnimation_Translate;private Animation myAnimation_Rotate; /根據(jù)各自的構(gòu)造方法來初始化一個(gè)實(shí)例對象myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);2. 分析: 1.1 AlphaAnimation第一步:AlphaAnimation類對象定義 private AlphaAnimation myAnimation_Alpha;第二步:AlphaAnimation類對象構(gòu)造AlphaAnimation(float fromAlpha, float toAlpha) /第一個(gè)參數(shù)fromAlpha為 動畫開始時(shí)候透明度/第二個(gè)參數(shù)toAlpha為 動畫結(jié)束時(shí)候透明度myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);/說明: / 0.0表示完全透明/ 1.0表示完全不透明第三步:設(shè)置動畫持續(xù)時(shí)間 myAnimation_Alpha.setDuration(5000);/設(shè)置時(shí)間持續(xù)時(shí)間為 5000毫秒1.2 ScaleAnimation第一步:ScaleAnimation類對象定義 private AlphaAnimation myAnimation_Scale;第二步:ScaleAnimation類對象構(gòu)造ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)/第一個(gè)參數(shù)fromX為動畫起始時(shí) X坐標(biāo)上的伸縮尺寸 /第二個(gè)參數(shù)toX為動畫結(jié)束時(shí) X坐標(biāo)上的伸縮尺寸 /第三個(gè)參數(shù)fromY為動畫起始時(shí)Y坐標(biāo)上的伸縮尺寸 /第四個(gè)參數(shù)toY為動畫結(jié)束時(shí)Y坐標(biāo)上的伸縮尺寸/*說明: 以上四種屬性值 0.0表示收縮到?jīng)]有 1.0表示正常無伸縮 值小于1.0表示收縮 值大于1.0表示放大*/第五個(gè)參數(shù)pivotXType為動畫在X軸相對于物件位置類型/第六個(gè)參數(shù)pivotXValue為動畫相對于物件的X坐標(biāo)的開始位置/第七個(gè)參數(shù)pivotXType為動畫在Y軸相對于物件位置類型 /第八個(gè)參數(shù)pivotYValue為動畫相對于物件的Y坐標(biāo)的開始位置myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);第三步:設(shè)置動畫持續(xù)時(shí)間myAnimation_Scale.setDuration(700);/設(shè)置時(shí)間持續(xù)時(shí)間為 700毫秒1.3 TranslateAnimation第一步:TranslateAnimation類對象定義 private AlphaAnimation myAnimation_Translate;第二步:TranslateAnimation類對象構(gòu)造 TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) /第一個(gè)參數(shù)fromXDelta為動畫起始時(shí) X坐標(biāo)上的移動位置/第二個(gè)參數(shù)toXDelta為動畫結(jié)束時(shí) X坐標(biāo)上的移動位置/第三個(gè)參數(shù)fromYDelta為動畫起始時(shí)Y坐標(biāo)上的移動位置/第四個(gè)參數(shù)toYDelta為動畫結(jié)束時(shí)Y坐標(biāo)上的移動位置 第三步:設(shè)置動畫持續(xù)時(shí)間 mmyAnimation_Translate.setDuration(2000);1.4 RotateAnimation第一步:RotateAnimation類對象定義private AlphaAnimation myAnimation_Rotate;第二步:RotateAnimation類對象構(gòu)造RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)/第一個(gè)參數(shù)fromDegrees為動畫起始時(shí)的旋轉(zhuǎn)角度/第二個(gè)參數(shù)toDegrees為動畫旋轉(zhuǎn)到的角度/第三個(gè)參數(shù)pivotXType為動畫在X軸相對于物件位置類型/第四個(gè)參數(shù)pivotXValue為動畫相對于物件的X坐標(biāo)的開始位置/第五個(gè)參數(shù)pivotXType為動畫在Y軸相對于物件位置類型/第六個(gè)參數(shù)pivotYValue為動畫相對于物件的Y坐標(biāo)的開始位置myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);第三步:設(shè)置動畫持續(xù)時(shí)間myAnimation_Rotate.setDuration(3000);五、如何使用java代碼中的動畫效果使用從View父類繼承過來的方法startAnimation()來為View或是子類View等等添加一個(gè)動畫效果public void startAnimation (Animation animation) - Frame動畫主要是通過AnimationDrawable類來實(shí)現(xiàn)的,它有start()和stop()兩個(gè)重要的方法來啟動和停止動畫。Frame動畫一般通過XML文件配置,在工程的res/anim目錄下創(chuàng)建一個(gè)XML配置文件,該配置文件有一個(gè)根元素和若干個(gè)子元素。 實(shí)現(xiàn)一個(gè)人跳舞的Frame動畫,6張圖片如下所示:1、把這6張圖片放到res/drawable目錄下,分別取名為:p01.png,p02.png,p03.png,p04.png,p05.png,p06.png。2、在res/anim目錄下創(chuàng)建一個(gè)XML配置文件,文件名為:dance.xml,文件內(nèi)容: apk:oneshot指示是否只運(yùn)行一次,設(shè)置為false則意味著循環(huán)播放。3、在res/layout目錄下創(chuàng)建layout配置文件dance.xml,文件內(nèi)容: apk:background使用上面的動畫作為背景,意味著

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論