Movie来播放GIF动画
摘自 DroidKe
Just Another Android Blog
播放GIF出現(xiàn)的花屏問題
2010/06/10, 16:48用Movie來播放gif動(dòng)畫時(shí),開始用android的SDK自帶的ApiDemos里的gif圖片是正常的,后來發(fā)現(xiàn)用上自己的gif圖片就出現(xiàn)了花屏的問題。
也許兩張gif圖片之間有什么設(shè)置差別造成的呢?于是我用Adobe ImageReady打開了能正常顯示的和出現(xiàn)花屏的gif圖片進(jìn)行對(duì)比。
結(jié)果找到來解決問題的方法(也許還有別的更好的方法)。
如下圖:
右擊動(dòng)畫幀,然后選擇“恢復(fù)為背景”。
標(biāo)簽: gif分類目錄: GUI??|? 評(píng)論
用Movie來播放GIF動(dòng)畫
2010/06/10, 16:21目前android.graphics.Movie類在SDK文檔中還沒有任何的說明。
Movie類是可以用來播放gif動(dòng)畫,跟電影的原理一樣都是通過播放連續(xù)的幀,利用視覺暫留得到運(yùn)動(dòng)的效果。這倒使我想到了AnimationDrawable類,這兩者看起來差不多,于是嘗試用Movie類來播放AnimationDrawable定義的動(dòng)畫,失敗了。
1,原理
通過不停的重新繪制來換幀。
2,方法說明
decodeByteArray(byte[] data, int offset, int length)
decodeFile(String pathName)
decodeStream(InputStream is)
三種不同的靜態(tài)方法來獲取Movie對(duì)象。三種方式的原理其實(shí)都一樣,都是讀入字節(jié)流來解碼創(chuàng)建Movie對(duì)象。decodeByteArray是從一個(gè)已經(jīng)存在的字節(jié)數(shù)組中創(chuàng)建,decodeFile是從一個(gè)文件讀入字節(jié)流來創(chuàng)建。
draw(Canvas canvas, float x, float y)
draw(Canvas canvas, float x, float y, Paint paint)
繪制動(dòng)畫幀。
它們其實(shí)還是調(diào)用Canvas的drawBitmap來繪制幀的。
duration()
動(dòng)畫持續(xù)的時(shí)間,也就是完成一次動(dòng)畫播放的時(shí)間。
isOpaque()
是不是不透明
setTime(int relativeMilliseconds)
設(shè)置相對(duì)本次播放第一幀時(shí)間,根據(jù)這個(gè)時(shí)間來決定顯示第幾幀。
例如:
int relTime=(int)((now – moviestart)%movie.duration());
now:系統(tǒng)當(dāng)前時(shí)刻
moviestart:動(dòng)畫開始的時(shí)間
movie.duration:動(dòng)畫持續(xù)的時(shí)間,也就是完成一次動(dòng)畫的時(shí)間
注意這是取余操作,這才能算出當(dāng)前這次重復(fù)播放的第一幀的時(shí)間。
height() 高
width() 寬
3,實(shí)例
public class Main extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new SampleView(this));}private static class SampleView extends View {private Movie mMovie;private long mStart=0;private int relTime;public SampleView(Context context) {super(context);mMovie = Movie.decodeStream(context.getResources().openRawResource(R.drawable.pig));}@Override protected void onDraw(Canvas canvas) {long now=android.os.SystemClock.uptimeMillis();if(mStart==0) mStart = now;int dur=mMovie.duration();if(dur==0) dur=1000;relTime=(int)((now-mStart)%dur);mMovie.setTime(relTime);canvas.drawColor(Color.WHITE);mMovie.draw(canvas, 0,0);this.invalidate();}} } 標(biāo)簽: gif, movie分類目錄: GUI??|? 1 條評(píng)論
對(duì)Animation的理解
2010/06/08, 14:11對(duì)android的Animation的初級(jí)理解,Moandroid的下面三篇文章已經(jīng)講解的很清楚了。
http://www.moandroid.com/?p=790
http://www.moandroid.com/?p=808
http://www.moandroid.com/?p=812
1,android的動(dòng)畫分為兩種:Tween Animation和Frame Animation。
這個(gè)兩種動(dòng)畫的原理也是不相同的,他們的包在SDK中的位置也是不一樣。Tween動(dòng)畫的包在android.view.animation下,Frame動(dòng)畫的包在android.graphics.drawable下。
2,這兩種動(dòng)畫的不同在于:
Frame動(dòng)畫是對(duì)預(yù)先設(shè)置好的圖片進(jìn)行依次顯示,以產(chǎn)生動(dòng)畫效果,就象電影那樣。
Tween動(dòng)畫則是要把它作用于某個(gè)對(duì)象上,如view組件,drawable。例如有個(gè)Button,想讓它產(chǎn)生轉(zhuǎn)換位置的動(dòng)畫效果,那么就要?jiǎng)?chuàng)建一個(gè)TranslateAnimation對(duì)象,然后把它作用于這個(gè)Button上。
3,那么TranslateAnimation是如何才能作用與Button上呢?
View對(duì)象都有一個(gè)setAnimation方法就是來和動(dòng)畫關(guān)聯(lián)起來產(chǎn)生動(dòng)畫效果。那么如果才能讓drawable也產(chǎn)生動(dòng)畫呢,drawable可沒有setAnimation方法來調(diào)用的。
android的View組件其實(shí)也是調(diào)用android.graphics.*包里的相關(guān)工具繪制出來的,也就是所View產(chǎn)生動(dòng)畫的原理同樣可以讓drawable產(chǎn)生動(dòng)畫。
4,Tween動(dòng)畫的原理
View組件是由Canvas來繪制出來的。
Canvas類包含一個(gè)Martix,在繪制時(shí)會(huì)先進(jìn)行一次矩陣運(yùn)算,把運(yùn)算的的結(jié)果顯示在Canvas上。這時(shí)圖片就產(chǎn)生了變動(dòng),圖形變換通過仿射矩陣實(shí)現(xiàn)。圖形變換是圖形學(xué)中的基本知識(shí)。簡(jiǎn)單來說就是,每種變換都是一次矩陣運(yùn)算。
Animation類有有個(gè)getTransformation(long currentTime, Transformation outTransformation)方法來獲取此動(dòng)畫當(dāng)前時(shí)刻的Transformation,它記錄了仿射矩陣 Matrix,動(dòng)畫每觸發(fā)一次,會(huì)對(duì)原來的矩陣做一次運(yùn)算。然后通過Transformation的getMatrix()方法把此Martix給提取出來。
然后讓Cavnas運(yùn)行此Martix,通過Canvas的concat(Matrix matrix)方法來替換當(dāng)前的martix。
到這里View按Animation的變換了一次,接下來就是要不停的從Animation中獲取Martix(如何變換的信息),Canvas不斷的改變,就產(chǎn)生的動(dòng)畫效果。
5,讓drawable產(chǎn)生Tween動(dòng)畫的例子
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | public class Main extends Activity {private Animation ani ;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new myView(this));}private class myView extends View{ private Drawable da;private ScaleAnimation sa;private int i=0;public myView(Context context) {super(context);// TODO Auto-generated constructor stub//創(chuàng)建drawableda =getResources().getDrawable(R.drawable.google);da.setBounds(0, 0,da.getIntrinsicWidth(), da.getIntrinsicHeight());//創(chuàng)建一個(gè)Animation,并設(shè)置。sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);sa.setDuration(3000);sa.setRepeatCount(1);sa.setRepeatMode(2);sa.startNow();}@Overrideprotected void onDraw(Canvas canvas){ //獲取TransformationTransformation tf = new Transformation();sa.getTransformation(AnimationUtils.currentAnimationTimeMillis(), tf);canvas.drawColor(Color.BLACK);//獲取Matrix并設(shè)置到canvascanvas.concat(tf.getMatrix()); da.draw(canvas); //這個(gè)是來設(shè)置不停的獲取Martix來重繪invalidate();}} } |
分類目錄: GUI??|? 評(píng)論
有關(guān)Intent的putExtra
2010/05/19, 12:25我們可以通過putExtra方法向Intent中添加數(shù)據(jù)。
在Intent類中有很多重載的putExtra方法。在android中Intent是通過Bundle類型來傳遞數(shù)據(jù)的,這些重載的方法獲取到參數(shù)后也是轉(zhuǎn)換成Bundle類型后才傳輸?shù)摹?/p>
例如其中一個(gè)方法的實(shí)現(xiàn):
public Intent putExtra(String name, byte value) { if (mExtras == null) { mExtras = new Bundle(); } mExtras.putByte(name, value); return this; } 如果想傳多組值,可以多次調(diào)用putExtra方法: intent1.putExtra(“name”,”liu”); intent1.putExtra(“city”,”beijing”); 當(dāng)然可以直接使用Bundle容器,然后調(diào)用?putExtras(Bundle ?extras) 標(biāo)簽: putExtra分類目錄: 基礎(chǔ)學(xué)習(xí)??|? 評(píng)論 ? Previous Entries
總結(jié)
以上是生活随笔為你收集整理的Movie来播放GIF动画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP新手入门简单了解PHP知识
- 下一篇: 光猫指示灯--路由器