Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to smooth out animation using SurfaceView?
    primarykey
    data
    text
    <pre><code>import android.os.Bundle; import android.app.Activity; 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; import android.view.View; import android.view.View.OnTouchListener; public class Play extends Activity implements OnTouchListener { MKZSurface ourSurfaceView; float x, y, sx, sy, fx, fy; float cW, cH; float ballx, bally; float blockx = 0; int counter = 0; int countx = 0, county = 0; int ballc; long startTime, currentTime, ballStartTime, ballTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ourSurfaceView = new MKZSurface(this); ourSurfaceView.setOnTouchListener(this); setContentView(ourSurfaceView); startTime = System.currentTimeMillis(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); ourSurfaceView.pause(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); ourSurfaceView.resume(); } @Override public boolean onTouch(View arg0, MotionEvent event) { // TODO Auto-generated method stub try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } x = event.getX(); y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: sx = event.getX(); sy = event.getY(); break; case MotionEvent.ACTION_UP: fx = event.getX(); fy = event.getY(); break; } return true; } public void svBackGround(Canvas canvas, Paint SolidC) { canvas.drawRect(0, (cH - 100), cW, cH, SolidC); canvas.drawRect(0, 0, cW, 100, SolidC); } public boolean ballJump() { if (sx != 0 &amp;&amp; sy != 0) { return true; } else { return false; } } public void Jump() { if (ballc == 0) { ballStartTime = System.currentTimeMillis(); ballc = 1; } if (ballc == 1) { ballTime = System.currentTimeMillis(); } if (county &gt;= -50 &amp;&amp; counter == 0 &amp;&amp; ballTime - ballStartTime &lt;= 1000) { county -= 10; } else if (county &lt;= -50 &amp;&amp; ballTime - ballStartTime &lt;= 1000) { counter = 1; } else if (ballTime - ballStartTime &gt;= 1000 &amp;&amp; county &lt; 0) { county += 10; counter = 1; } if (ballTime - ballStartTime &gt;= 250 &amp;&amp; county == 0 &amp;&amp; counter == 1) { sx = 0; sy = 0; ballc = 0; counter = 0; } } public class MKZSurface extends SurfaceView implements Runnable { SurfaceHolder ourHolder; Thread ourThread = null; boolean isRunning = false; public MKZSurface(Context context) { super(context); ourHolder = getHolder(); // TODO Auto-generated } public void pause() { isRunning = false; while (true) { try { ourThread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } ourThread = null; } public void resume() { isRunning = true; ourThread = new Thread(this); ourThread.start(); } @Override public void run() { // TODO Auto-generated method stub while (isRunning) { if (!ourHolder.getSurface().isValid()) continue; Canvas canvas = ourHolder.lockCanvas(); canvas.drawRGB(255, 255, 255); Paint SolidC = new Paint(); SolidC.setColor(Color.LTGRAY); Paint BallC = new Paint(); BallC.setColor(Color.CYAN); if (ballJump() == true) { Jump(); } cW = canvas.getWidth(); cH = canvas.getHeight(); ballx += 10; bally = (cH - 115) + county; canvas.drawCircle(ballx, bally, 15, BallC); svBackGround(canvas, SolidC); ourHolder.unlockCanvasAndPost(canvas); } } } </code></pre> <p>The animation isn't smooth and doesnt fade in with each other, I've tried to increase the frame rate but it doesn't help and I've reduced the distance. How do i make the animation smoother? It just recreates the circle at a different point, and doesn't show it move. How do I make the ball seem as it is moving?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload