Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.chronosx88.testmodule;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- class TestSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
- int count;
- boolean isClicked = false;
- public TestSurfaceView(Context context) {
- super(context);
- getHolder().addCallback(this);
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- new DrawThread(holder).start();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- count++;
- if(count == 3) {
- count = 0;
- }
- isClicked = true;
- return super.onTouchEvent(event);
- }
- class DrawThread extends Thread {
- SurfaceHolder surfaceHolder;
- boolean isRun = true;
- public DrawThread(SurfaceHolder surfaceHolder) {
- this.surfaceHolder = surfaceHolder;
- }
- @Override
- public void run() {
- while(isRun) {
- Canvas canvas = surfaceHolder.lockCanvas();
- if(canvas != null) {
- canvas.drawColor(Color.RED);
- if(isClicked) {
- switch(count) {
- case 0: {
- canvas.drawColor(Color.RED);
- break;
- }
- case 1: {
- canvas.drawColor(Color.GREEN);
- break;
- }
- case 2: {
- canvas.drawColor(Color.BLUE);
- }
- }
- }
- surfaceHolder.unlockCanvasAndPost(canvas);
- }
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement