当前位置 : 主页 > 编程语言 > c++ >

动态曲线 Timer.java

来源:互联网 收集:自由互联 发布时间:2021-07-03
Timer 实现 public class SurfaceViewTest extends SurfaceView implements SurfaceHolder.Callback { private Timer timer; SurfaceHolder holder; private MyTimerTask timerTask;//----------------------------- private final int HEIGHT = 800; // 要绘
Timer 实现
public class SurfaceViewTest extends SurfaceView implements SurfaceHolder.Callback {
    private Timer timer;
    SurfaceHolder holder;
    private MyTimerTask timerTask;
//-----------------------------
    private final int HEIGHT = 800;
    // 要绘制的曲线的水平宽度
    private final int WIDTH = 1000;
    // 离屏幕左边界的起始距离
    private final int X_OFFSET = 5;
    // 初始化X坐标
    private int cx = X_OFFSET;
    // 实际的Y轴的位置
    private int centerY = HEIGHT / 2;
    private int cy = 0;

    public SurfaceViewTest(Context context) {
        super(context);
        init();
    }

    public SurfaceViewTest(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        holder = getHolder();
        holder.addCallback(this);
        // 设置画布 背景透明
        holder.setFormat(PixelFormat.TRANSLUCENT);
        // --焦点设置----------------------------
        setFocusable(true);
        // 设置触屏
        setFocusableInTouchMode(true);
        // 设置常亮
        setKeepScreenOn(true);
        timer=new Timer();
        timerTask = new MyTimerTask();
    }
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        timer.schedule(timerTask, 30, 50);
    }
    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        timer.cancel();
        timer.purge();
    }

    class MyTimerTask extends TimerTask {
        Paint paint;
        Canvas c = null;
        int deltax = 0;
        int count = 0;
        private Queue
 
   queue;
        public MyTimerTask() {
            paint = new Paint();
            paint.setColor(Color.BLUE);
            paint.setStyle(Paint.Style.STROKE);// 空心
            paint.setStrokeWidth(2);
            paint.setTextSize(25);
            queue= new LinkedList
  
   (); } @Override public void run() { cy = (int) (Math.sin((cx - X_OFFSET) / 20f) * 300) + centerY; queue.add(new Point(cx, cy)); cx++; count++; if (cx > 1000) { deltax++; queue.poll(); } if (count % 1 == 0) { // 提交修改 Canvas canvas = holder.lockCanvas(new Rect(0, 0, HEIGHT, WIDTH)); canvas.drawColor(Color.WHITE); canvas.drawLine(X_OFFSET, centerY, WIDTH, centerY, paint); canvas.drawLine(X_OFFSET, 40, X_OFFSET, HEIGHT, paint); for (int i=0;i<100;i++){ canvas.drawText(""+i*10,X_OFFSET+i*50,centerY,paint); } // 根据X,Y坐标画点 List
   
     list = (List
    
     ) queue; Point prev = list.get(0); for (int i = 1; i < list.size(); i++) { Point cur = list.get(i); canvas.drawLine(prev.x - deltax, prev.y, cur.x - deltax, cur.y, paint); prev = cur; } holder.unlockCanvasAndPost(canvas); } } }; }
    
   
  
 
线程实现
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    LoopThread thread;
    public MySurfaceView(Context context) {
        super(context);
        init();
    }
    private void init() {
        SurfaceHolder holder=getHolder();
        holder.addCallback(this);
        // 设置画布 背景透明
        holder.setFormat(PixelFormat.TRANSLUCENT);
        // --焦点设置----------------------------
        setFocusable(true);
        // 设置触屏
        setFocusableInTouchMode(true);
        // 设置常亮
        setKeepScreenOn(true);
        thread=new LoopThread(holder,getContext());
    }
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
           thread.isRunning=true;
           thread.start();
    }
    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
         thread.isRunning=false;
        try {
            thread.join();
            //Java Thread中,
            // join() 方法主要是让调用该方法的thread完成run方法里面的东西后, 再执行join()方法后面的代码。示例:
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    class LoopThread extends Thread{
        SurfaceHolder surfaceHolder;
        Context context;
        boolean isRunning;
        float radius=10f;
        Paint paint;

        public LoopThread(SurfaceHolder surfaceHolder,Context context){
            this.surfaceHolder=surfaceHolder;
            this.context=context;
            isRunning=false;
            paint=new Paint();
            paint.setColor(Color.YELLOW);
            paint.setStyle(Paint.Style.STROKE);// 空心

        }

        @Override
        public void run() {
            super.run();
            Canvas c=null;
            while (isRunning){
                try {
                    synchronized(surfaceHolder){
                        c=surfaceHolder.lockCanvas(null);
                        doDraw(c);
                        //
                        Thread.sleep(50);

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }


        }

        public void doDraw(Canvas c) {
            //这个很重要,清屏操作,清楚掉上次绘制的残留图像
            c.drawColor(Color.BLACK);   /// surfaceView 默认为黑色背景;
            c.translate(200,200);
            c.drawCircle(0,0,radius++,paint);
            if (radius>100)
            {
                radius=10f;
            }
        }
    }
}
网友评论