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); } }
SurfaceViewusesCanvasfor 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