There is a button, I click on it and move around the screen, but when I quickly move my finger across the screen, this button is noticeably lagging behind the finger. I conclude that the button does not have time to quickly draw or the screen refresh rate is not sufficient. How can this problem be solved? what means it is better to draw using Canvas, Surface view or something else? The question is naturally about java.

public class MainActivity1 extends Activity { public static float touchX = 50,touchY= 50; private float korX = touchX, korY = touchY, initX=0, initY=0; static int width; static int heigth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // GraphicsView myview = new GraphicsView(this); setContentView(new GraphicsView(this)); display(); } public void display(){ width = getWindowManager().getDefaultDisplay().getWidth(); heigth = getWindowManager().getDefaultDisplay().getHeight(); return; } public class GraphicsView extends SurfaceView implements SurfaceHolder.Callback { private DrawThread drawThread; public GraphicsView(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(),getResources()); 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) { } } } public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: korX = touchX; korY = touchY; initX = event.getRawX(); initY = event.getRawY(); return true; case MotionEvent.ACTION_MOVE: touchX = (korX + (event.getRawX() - initX)); touchY = (korY + (event.getRawY() - initY)); return true; } return false; } } 

The flow in which the drawing is going

  public class DrawThread extends Thread { private boolean running = false; private SurfaceHolder surfaceHolder; private Matrix matrix; private long prevTime; public Bitmap bitmap; public DrawThread(SurfaceHolder surfaceHolder,Resources resources){ this.surfaceHolder = surfaceHolder; bitmap = BitmapFactory.decodeResource(resources, R.drawable.face_box); } public void setRunning(boolean running){ this.running = running; } @Override public void run(){ while (running){ Canvas canvas = null; try { canvas = surfaceHolder.lockCanvas(null); if (canvas == null) continue; synchronized (surfaceHolder) { MainActivity1.touchY, null); canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); onDraw(canvas); } } finally { if (canvas != null){ surfaceHolder.unlockCanvasAndPost(canvas); } } } } protected void onDraw(Canvas canvas) { canvas.drawBitmap(bitmap, MainActivity1.touchX, MainActivity1.touchY, null); } } 
  • one
    SurfaceView uses Canvas for rendering, it makes no sense to compare them. Attach the button drawing code, if this is your custom button, or the code with which you change the position of the button in the space, most likely there is something wrong PS Moving the interface elements is a very non-standard user experience. Think about it, maybe you are trying to create functionality only for the sake of functionality, since users are unlikely to appreciate - P. Ilyin
  • one
    @ P.Ilyin Let me disagree. As for me, this is the standard Drag'N'Drop, which is already about 30 years old. In addition, any Android user sooner or later confronts with setting up widgets on the screen, which is dragging items for convenience. - Alex Krass
  • Rather, it is a matter of comparing touch coordinates and button position coordinates, as mentioned above. Check the correction of the position of the button depending on its length / width. Not once encountered such behavior. - DimXenon
  • Dragging homeScreen elements is one thing, quite another is to change the position of UI elements in the application. Facebook, for example, was downloaded 1 billion times and there is nothing like it. The only thing that comes to mind is dragging audio recordings to the VC, but there it is quite justified (the user wants a different playback order). As a result, we will postpone this dispute and you will still attach a part of the code - P. Ilyin
  • Yes, and indeed I had to tell you what I want to implement. The problem is this. There is a ball on the screen, I want to move my finger across the screen, but when I start moving my finger in an accelerated motion, this ball is lagging behind. And the goal is to make a game where this ball runs away from other balls moving on the screen (Game like HARD or igeny, you should be familiar with it) In general, questions about whether you need such a functionality disappear. onTouchEvent gets the coordinates that are passed to onDraw (Canvas canvas) for drawing. I will attach the code to the question, maybe there is a problem in it. - Turalllb

0