如何让Android支持GIF图片

public class SampleView extends View {
 
private Movie mMovie;
 
private Bitmap mBitmap;
 
private Canvas mCanvas;
 
private long mMovieStart;
 
   private  byte[] streamToBytes(InputStream is) {
 
            ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
 
            byte[] buffer = new byte[1024];
 
            int len;
 
            try {
 
                while ((len = is.read(buffer)) >= 0) {
 
                    os.write(buffer, 0, len);
 
                }
 
            } catch (java.io.IOException e) {
 
            }
 
            return os.toByteArray();
 
        }
 
public SampleView(Context context) {
 
super(context);
 
  
 
setFocusable(true);
 
  
 
InputStream is;
 
  
 
is = context.getResources().openRawResource(R.drawable.aa_gif);
 
  
 
if (true) {
 
             mMovie = Movie.decodeStream(is);
 
         } else {
 
             byte[] array = streamToBytes(is);
 
             mMovie = Movie.decodeByteArray(array, 0, array.length);
 
         }
 
 
int w = mMovie.width();
 
         int h = mMovie.height();
 
         Log.i("-------width-------", w+"");
 
         Log.i("-------height-------", h+"");
 
       // int[] pixels = new int[w*h];
 
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
 
         mCanvas = new Canvas(mBitmap);
 
  
 
         }
 
  
    @Override
 
    protected void onDraw(Canvas canvas) {
 
        canvas.drawColor(0xFFCCCCCC);            
 
  
 
        Paint p = new Paint();
 
        p.setAntiAlias(true);
 
 
        long now = android.os.SystemClock.uptimeMillis();
 
        if (mMovieStart == 0) {   // first time
 
            mMovieStart = now;
 
        }
 
        if (mMovie != null) {
 
            int dur = mMovie.duration();
 
            if (dur == 0) {
 
                dur = 1000;
 
            }
 
            int relTime = (int)((now - mMovieStart) % dur);
 
            mMovie.setTime(relTime);
 
            mCanvas.drawColor(0xFFCCCCCC);
 
            mMovie.draw(mCanvas, 0, 0);
 
            Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, 300, 300, false);
 
  
            mBitmap.createScaledBitmap(mBitmap, 200, 300, false);
 
            canvas.drawBitmap(bitmap, 100, 200, null);
 
            invalidate();
 
        }
 
    }
 
}
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SampleView sampleView = new SampleView(this);
        setContentView(sampleView);
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值