android使用movie显示gif图片的自定义view

android使用movie显示gif图片的自定义view
说明:因为代码比较简单,属于网上其他地方转载的,不做其他解释。
转载地址:http://blog.csdn.net/fkaking/article/details/12106655
核心:将gif传入movie中,然后在ondraw中做循环播放即可。

package com.patrick.gifdemo.customView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class GifView extends View {
    private int gifResource;
    private Movie mMovie;
    private long movieStart;

    public GifView(Context context, byte[] data) {
        super(context);
        // initializeView();
        setGIFResource(data);
    }

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

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

    /**
     * 初始化控件
     */
    private void initializeView() {
        if (gifResource != 0) {
            InputStream is = getContext().getResources().openRawResource(gifResource);
            byte[] array = streamToBytes(is);
            mMovie = Movie.decodeByteArray(array, 0, array.length);
            movieStart = 0;
            this.invalidate();
        }
    }

    /**
     * 设置GIF资源ID
     *
     * @param id
     */
    public void setGIFResource(int id) {
        this.gifResource = id;
        initializeView();
    }

    /**
     * 获取资源ID
     *
     * @return
     */
    public int getGIFResoure() {
        return gifResource;
    }

    /**
     * 设置GIF资源数据
     *
     * @param bytes
     */
    public void setGIFResource(byte[] bytes) {
        if (bytes == null || bytes.length <= 0) {
            return;
        }
        mMovie = Movie.decodeByteArray(bytes, 0, bytes.length);
        movieStart = 0;
        this.invalidate();
    }

    /**
     * 设置GIF资源数据
     *
     * @param is
     */
    public void setGIFResource(InputStream is) {
        if (is == null) {
            return;
        }
        byte[] array = streamToBytes(is);
        mMovie = Movie.decodeByteArray(array, 0, array.length);
        movieStart = 0;
        this.invalidate();
    }

    /**
     * 设置GIF资源图片
     *
     * @param bitmap
     */
    public void setGIFResource(Bitmap bitmap) {
        if (bitmap == null) {
            return;
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
        byte[] bitmapdata = bos.toByteArray();
        mMovie = Movie.decodeByteArray(bitmapdata, 0, bitmapdata.length);
        movieStart = 0;
        this.invalidate();
    }

    private static 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();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        canvas.drawColor(Color.TRANSPARENT);
        super.onDraw(canvas);
        long now = android.os.SystemClock.uptimeMillis();
        if (movieStart == 0) {
            movieStart = now;
        }
        int d = mMovie.duration();
        if (d <= 0) {
            d = 1500;
        }
        if (mMovie != null) {
            int relTime = (int) ((now - movieStart) % d);
            mMovie.setTime(relTime);
            mMovie.draw(canvas, (getWidth() - mMovie.width()) / 2, (getHeight() - mMovie.height()) / 2);
            this.invalidate();
        }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值