自定义SurfaceView之加载GIF

一些说明:

1.简单的使用 iamgeview不能加载gif图片

2.对于自定义view的生命周期是:view的构造参数–>activity的oncreate–>surfaceCreated()–>…

3.holder.addCallback(this);//增加了callback才会执行surfaceCreated()

4.如果gif图片过大会导致内存溢出

xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
   >

 <com.example.project1205.MySurfaceView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/sur">

</com.example.project1205.MySurfaceView> 

</FrameLayout>

MainActivity

package com.example.project1205;

import android.os.Bundle;
import android.app.Activity;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MySurfaceView surfaceView = (MySurfaceView) this.findViewById(R.id.sur);
        surfaceView.setFileNmae("g1.gif");//选中GIF
        surfaceView.setScale(3.0f);//设置空间和GIF的缩放程度
    }
}

MySurfaceView

package com.example.project1205;

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

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements Callback{
    SurfaceHolder holder;//持有人
    String fileNmae ;//GIF图片名称
    Movie movie;//GIF的载体
    Handler handler ;
    Runnable runnable;
    int screenWdith ;
    int screenHeight ;
    float scale ;//缩放的倍数


    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化参数
        screenWdith = context.getResources().getDisplayMetrics().widthPixels;
        screenHeight = context.getResources().getDisplayMetrics().heightPixels;
        holder = getHolder();//实例holder
        holder.addCallback(this);//增加了callback才会执行surfaceCreated()
        handler = new Handler();

        runnable = new Runnable() {
            @Override
            public void run() {
                Canvas lockCanvas = holder.lockCanvas();//获得加锁画布
                lockCanvas.scale(scale, scale);//让画布缩放

        /*      movie.draw(lockCanvas, screenWdith/2-movie.width()/2, screenHeight/2-movie.height()/2);//画,居中显示(背景是黑色的)    */
                movie.draw(lockCanvas,0,0);
                movie.setTime((int) (System.currentTimeMillis()%movie.duration()));
                holder.unlockCanvasAndPost(lockCanvas);//解锁画布
                handler.postDelayed(runnable, 50);//每个50毫秒切换一张图片
            }
        };
    }


    //设置控件的宽和长
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        try {
            InputStream inputStream = getContext().getAssets().open(fileNmae);//获得GIF的内容,说明GIF要放在assets文件夹下
            movie = Movie.decodeStream(inputStream);//将GIF的内容放入载体中

            int width = movie.width();
            int height = movie.height();
            this.setMeasuredDimension((int) (width * scale), (int) (height * scale));//将surfaceview的大小设置为GIF图的大小,这样xml的center才有效

            handler.post(runnable);//开始动画
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    //如果不实现Callback接口,将不能重写这3个方法
    @Override
    public void surfaceCreated(SurfaceHolder arg0) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }



    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        //停止动画
        handler.removeCallbacks(runnable);

    }


    public String getFileNmae() {
        return fileNmae;
    }


    public void setFileNmae(String fileNmae) {
        this.fileNmae = fileNmae;
    }

    public float getScale() {
        return scale;
    }


    public void setScale(float scale) {
        this.scale = scale;
    }
}

效果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值