Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package io.github.chronosx88.testmodule;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.view.MotionEvent;
  8. import android.view.SurfaceHolder;
  9. import android.view.SurfaceView;
  10.  
  11. class TestSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
  12.  
  13.     int count;
  14.     boolean isClicked = false;
  15.  
  16.     public TestSurfaceView(Context context) {
  17.         super(context);
  18.         getHolder().addCallback(this);
  19.     }
  20.  
  21.     @Override
  22.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  23.  
  24.     }
  25.  
  26.     @Override
  27.     public void surfaceCreated(SurfaceHolder holder) {
  28.         new DrawThread(holder).start();
  29.     }
  30.  
  31.     @Override
  32.     public void surfaceDestroyed(SurfaceHolder holder) {
  33.  
  34.     }
  35.  
  36.     @Override
  37.     public boolean onTouchEvent(MotionEvent event) {
  38.         count++;
  39.         if(count == 3) {
  40.             count = 0;
  41.         }
  42.         isClicked = true;
  43.         return super.onTouchEvent(event);
  44.     }
  45.  
  46.     class DrawThread extends Thread {
  47.         SurfaceHolder surfaceHolder;
  48.         boolean isRun = true;
  49.  
  50.         public DrawThread(SurfaceHolder surfaceHolder) {
  51.             this.surfaceHolder = surfaceHolder;
  52.         }
  53.  
  54.         @Override
  55.         public void run() {
  56.             while(isRun) {
  57.                 Canvas canvas = surfaceHolder.lockCanvas();
  58.                 if(canvas != null) {
  59.                     canvas.drawColor(Color.RED);
  60.                     if(isClicked) {
  61.                         switch(count) {
  62.                             case 0: {
  63.                                 canvas.drawColor(Color.RED);
  64.                                 break;
  65.                             }
  66.                             case 1: {
  67.                                 canvas.drawColor(Color.GREEN);
  68.                                 break;
  69.                             }
  70.                             case 2: {
  71.                                 canvas.drawColor(Color.BLUE);
  72.                             }
  73.                         }
  74.                     }
  75.                     surfaceHolder.unlockCanvasAndPost(canvas);
  76.                 }
  77.  
  78.                 try {
  79.                     sleep(1000);
  80.                 } catch (InterruptedException e) {
  81.                     e.printStackTrace();
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement