Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android gif播放

android gif播放

編輯:關於Android編程

package com.ttw.gif;


import java.io.IOException;
import java.io.InputStream;


import com.ttw.androidhtppclient.Utility;


import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;


public class GifMove extends View {


private Movie mMovie;


private InputStream is = null;

private long mMovieStart;


public GifMove(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


public GifMove(Context context, AttributeSet attrs) {
super(context, attrs);
}


public GifMove(Context context) {
super(context);
}


public void setGifSource(int source) {

BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), source, option);
Utility.logI("move widht = " + option.outWidth + " height = " + option.outHeight);
LayoutParams lp = this.getLayoutParams();
lp.width = option.outWidth;
lp.height = option.outHeight;
this.setLayoutParams(lp);
if(null != is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
is = null;
}
}
is = getResources().openRawResource(source);
mMovie = Movie.decodeStream(is);
}


@Override
protected void onDraw(Canvas canvas) {
long now = android.os.SystemClock.uptimeMillis();
 
         if (mMovieStart == 0) {
             mMovieStart = now;
         }

if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 500;
}
int relTime = (int) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight()
- mMovie.height());
invalidate();
}
super.onDraw(canvas);
}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved