Android中加载GIF图片及遇到的问题

package com.qinqu.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Paint;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.qinqu.R;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GifMovieNetView extends View {
    private static final String TAG = GifMovieNetView.class.getSimpleName();
    private Movie mMovie;
    private long mPlayMovieTime;
    private Context context;
    private int mMovieResourceId;

    public GifMovieNetView(Context context) {
        super(context);
        this.context = context;
    }

    public GifMovieNetView(Context context, AttributeSet attrs) {
        this(context, attrs, R.styleable.CustomsTheme_gifMoviewViewStyle);
    }

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

    @SuppressLint("NewApi")
    private void setViewAttributes(Context context, AttributeSet attrs, int defStyle) {


        final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GifMoviewView, defStyle,
                R.style.Widget_GifMoviewView);
        mMovieResourceId = array.getResourceId(R.styleable.GifMoviewView_gif, -1);
//        mPaused = array.getBoolean(R.styleable.GifMoviewView_paused, false);

        array.recycle();

        if (mMovieResourceId != -1) {
            mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
        }
    }


    public void readGifFormNative(int movieResId) {
        this.mMovieResourceId = movieResId;
        mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
        requestLayout();
    }


    public void readGifFormNet(final String path) {
        mMovie = Movie.decodeFile(path);
        if (mMovie != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            /**要禁止硬件加速
             * Starting from HONEYCOMB have to turn off HW acceleration to draw
             * Movie on Canvas.
             */
            this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
       /* new Thread(new Runnable() {
            @Override
            public void run() {
                httpTest(path);
                mHandler.sendEmptyMessage(0);
            }
        }).start();*/
    }


    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    invalidate();
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };


    private void httpTest(String path) {
        try {
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
// connection.setRequestMethod("GET");
            int size = connection.getContentLength();
            Log.e(TAG, "size = " + size);
            InputStream in = connection.getInputStream();
            byte[] array = streamToBytes(in);
            mMovie = Movie.decodeByteArray(array, 0, array.length);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    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) {
        Paint p = new Paint();
        p.setAntiAlias(true);
        setLayerType(LAYER_TYPE_SOFTWARE, p);
        if (mMovie != null) {
            long now = android.os.SystemClock.uptimeMillis();
            if (mPlayMovieTime == 0) {   // first time
                mPlayMovieTime = now;
            }
            int dur = mMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }
            int relTime = (int) ((now - mPlayMovieTime) % dur);
            mMovie.setTime(relTime);
            mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2,
                    getHeight() / 2 - mMovie.height() / 2);//让图片显示在中间
            invalidate();//让程序重画
        }
    }

}

遇到的问题:

有些手机加载不出来报错:

11-15 11:23:51.883 14848-14848/com.mker A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 14848 (com.mker)

解决:

  mMovie = Movie.decodeFile(path);
        if (mMovie != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            /**要禁止硬件加速
             * Starting from HONEYCOMB have to turn off HW acceleration to draw
             * Movie on Canvas.
             */
            this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }

demo链接

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值