温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SurfaceView 连续渲染 SurfaceHolder   Canvas

发布时间:2020-05-18 00:43:35 来源:网络 阅读:417 作者:just2012xia 栏目:移动开发
Canvas canvas = holder.lockCanvas();
canvas.drawRGB(255, 0, 0);
holder.unlockCanvasAndPost(canvas);

第一行,锁定Surface用于渲染并返回一个可用的Canvas

第二行,解锁Surface并确保通过Canvas进行绘制的内容可显示到屏幕上


例子:

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends Activity {
      
    FastRenderView renderView;
      
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        renderView = new FastRenderView(this);
        setContentView(renderView);
    }
    protected void onResume() {//重载
        super.onResume();
        renderView.resume();
    }
    class FastRenderView extends SurfaceView implements Runnable
    {
        Thread renderThread = null;
        SurfaceHolder holder = null;
        volatile boolean running = false;
        public FastRenderView(Context context) {
            super(context);
            holder = getHolder();
        }
        public void resume()//自定义
        {
            running = true;
            renderThread.start();
        }
        public void run()
        {
            while(running)
            {
                if(!holder.getSurface().isValid())
                    continue;
                Canvas canvas = holder.lockCanvas();
                canvas.drawRGB(255, 0, 0);
                holder.unlockCanvasAndPost(canvas);
            }
        }
        public void pause()//自定义
        {
            running = false;
            while(true)
            {
                try {
                    renderThread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI