Good day!
I'm trying to implement drawing objects in SurfaceView by touch . In this case, each touch generates a separate thread, in which the object with acceleration falls down.
The question arose whether it is possible in the flow for SurfaceView to create a delay so that it does not interfere with other flows? Now she draws instantly all the stages of movement of the figure.
Code:
class DrawView extends SurfaceView implements SurfaceHolder.Callback { private DrawThread drawThread; boolean Draw = false; public float X1, Y1, evX,evY; public DrawView(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) { drawThread = new DrawThread(getHolder()); drawThread.setRunning(true); drawThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; drawThread.setRunning(false); while (retry) { try { drawThread.join(); retry = false; } catch (InterruptedException e) { } } } @Override public boolean onTouchEvent(MotionEvent event) { evX = event.getX(); evY = event.getY(); int action = event.getAction(); switch(action){ case MotionEvent.ACTION_DOWN: this.surfaceCreated(getHolder()); Draw = true; X1 = evX; Y1 = evY; break; default: } return true; //processed } class DrawThread extends Thread { public Paint mPaint = new Paint(); private boolean running = false; private SurfaceHolder surfaceHolder; int c = 1; public DrawThread(SurfaceHolder surfaceHolder) { this.surfaceHolder = surfaceHolder; } public void setRunning(boolean running) { this.running = running; } public void DrawOkr(Canvas mcanvas, float X, float Y){ float pX1, pX2, pX3, pX4; float pY1, pY2, pY3, pY4; mPaint = new Paint(); mPaint.setColor(Color.RED); pX1 = X - 15; pY1 = Y; pX2 = X; pY2 = Y - 30; mcanvas.drawLine(pX1, pY1, pX2, pY2, mPaint); pX3 = X + 15; pY3 = Y; mcanvas.drawLine(pX2, pY2, pX3, pY3, mPaint); pX4 = X; pY4 = Y + 30; mcanvas.drawLine(pX3, pY3, pX4, pY4, mPaint); mcanvas.drawLine(pX4, pY4, pX1, pY1, mPaint); mcanvas.drawLine(pX1, pY1, pX3, pY3, mPaint); } @Override public void run() { Canvas canvas; while (running) { canvas = null; try { canvas = surfaceHolder.lockCanvas(null); synchronized (surfaceHolder) { if (canvas == null) continue; DrawOkr(canvas, evX, evY); c = 1; while(evY < canvas.getHeight()) { evY = evY + 10; if (evY > canvas.getHeight()) { if (c <= 3) { evX = evX + 15; evY = (canvas.getHeight() - (60/c)); c++; } else { evY = canvas.getHeight(); } } DrawOkr(canvas, evX, evY); } running = false; } } finally { if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } } } } }