美文网首页
SurfaceView模板

SurfaceView模板

作者: 前端1号站 | 来源:发表于2018-03-20 10:22 被阅读0次
package com.qiaoguo.surfaceviewproject;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Administrator on 2018/3/20.
 */

public class SurfaceViewTemplate extends SurfaceView implements Runnable {
    private Thread mThread;
    private volatile  boolean isRunning;

    public SurfaceViewTemplate(Context context, AttributeSet attrs) {
        super(context, attrs);
        SurfaceHolder holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                mThread = new Thread(SurfaceViewTemplate.this);
                mThread.start();

            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
         isRunning=false;
            }
        });


    }

    @Override
    public void run() {
        while (isRunning) {
            drawSelf();
        }
    }

    private void drawSelf() {
        Canvas canvas=null;
        try {
            getHolder().lockCanvas();
            if (canvas!=null){
//绘制

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (canvas!=null){
                getHolder().unlockCanvasAndPost(canvas);
            }
        }

    }
}

相关文章

网友评论

      本文标题:SurfaceView模板

      本文链接:https://www.haomeiwen.com/subject/kxevqftx.html