zoukankan      html  css  js  c++  java
  • 18_SurfaceView 其他线程绘图

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Paint.Style;
    import android.os.Bundle;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.Window;
    import android.view.WindowManager;

    public class MainActivity extends Activity {     
     
        FastRenderView renderView;           
           
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
            renderView = new FastRenderView(this);
            setContentView(renderView);
        }     
       
        protected void onResume() {
            super.onResume();
            renderView.resume();
        }
       
        protected void onPause() {
            super.onPause();        
            renderView.pause();
        }   
     
    }


    class FastRenderView extends SurfaceView implements Runnable {
        Thread renderThread = null;
        SurfaceHolder holder;
        volatile boolean running = false;
       
        public FastRenderView(Context context) {
            super(context);          
            holder = getHolder();           
        }

        public void resume() {         
            running = true;
            renderThread = new Thread(this);
            renderThread.start();        
        }
       
        public void pause() {       
            running = false;                       
            while(true) {
                try {
                    renderThread.join();
                    break;
                } catch (InterruptedException e) {
                    // retry
                }
            }
            renderThread = null;       
        }
       
        public void run() {
            while(running) { 
                if(!holder.getSurface().isValid())
                    continue;
               
                Canvas canvas = holder.lockCanvas();           
                drawSurface(canvas);                                          
                holder.unlockCanvasAndPost(canvas);           
            }
        }       
       
        private void drawSurface(Canvas canvas) {
         Paint paint =  new Paint();
         paint.setColor(Color.RED);
            canvas.drawRGB(255, 255, 0);
            canvas.drawLine(0, 0, 100, 100,paint);
            canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 40, paint);
            paint.setStyle(Style.FILL);
            paint.setColor(0x770000ff);
            canvas.drawRect(100, 100, 200, 200, paint);
        }
    }  

  • 相关阅读:
    html页面中的转意字符
    bootstrap学习笔记3- navbar-header navbar-toggle 类 data-toggle和data-target
    DIV嵌套过程中的高度自适应问题
    <span class="icon-bar"></span> 不显示?
    CSS中定位的浮动float
    CSS 盒模型,块级元素和行内元素的区别和特性
    CSS padding
    CSS Position(定位)
    网络资料
    vue中 具名插槽+作用域插槽的混合使用
  • 原文地址:https://www.cnblogs.com/xl711436/p/3060789.html
Copyright © 2011-2022 走看看