0

I'm trying to create moving text.I have crated surface view and thread for looping it.but it not show me a moving motion but draw that text each after like * ** * * in infinite.

But what I need was move that this point to that** -> ** got it?

this is my code

package com.CurvePackage.Curve;

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.widget.Toast;

public class Origin extends SurfaceView implements SurfaceHolder.Callback {
    Context context1;
    private MainThread thread;
    private int x=0;
    private int y=0;


    public Origin(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setWillNotDraw(false);
        context1 = context;
        getHolder().addCallback(this);
        thread = new MainThread(getHolder(), this);

        setFocusable(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setTextSize(23);
        paint.setFakeBoldText(true);
        paint.setColor(Color.YELLOW);
        // int score=(10-sprites.size()*100);
        x=x+20;
        y=y+20;
        canvas.drawText("ewqewqe", x, y, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return super.onTouchEvent(event);
        // Toast.makeText(getba, "Replay clicked!", Toast.LENGTH_SHORT).show();
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        thread.setRunning(true);
        thread.start();

    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        thread.setRunning(false);
        // TODO Auto-generated method stub
//      boolean retry = true;
//      while (retry) {
//          try {
//              thread.join();
//              retry = false;
//          } catch (InterruptedException e) {
//              // try again shutting down the thread
//          }
//      }

    }
}

Main Thread

package com.CurvePackage.Curve;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread {

    // flag to hold game state
    private boolean running;
    private SurfaceHolder surfaceHolder;
    private Origin origin;
     static final long FPS = 15;

    public void setRunning(boolean running) {
        this.running = running;
    }

    @Override
    public void run() {

        long ticksPS = 1000 / FPS;
        long startTime;
        long sleepTime;

        while (running) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                   c = origin.getHolder().lockCanvas();
                   synchronized (origin.getHolder()) {
                       origin.onDraw(c);
                   }
            } finally {
                   if (c != null) {
                       origin.getHolder().unlockCanvasAndPost(c);
                   }
            }


            sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
            try {
                   if (sleepTime > 0)
                          sleep(sleepTime);
                   else
                          sleep(10);
            } catch (Exception e) {}
        }

    }

    public MainThread(SurfaceHolder surfaceHolder, Origin origin) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.origin = origin;
    }

}

1 Answers1

0

You need to paint the entire background in your onDraw method, before you paint your text.

Paint p2 = new Paint();
p2.setStyle(Style.FILL);
canvas.drawRect(0, 0, screenWidth, screenHeight, p2);
//draw text here

This will completely paint over the text from the previous time the canvas was drawn, and will remove this dragging effect.

user11559
  • 28
  • 1
  • 6
  • Is this the perfect solution for this? – FriendOnline May 25 '12 at 05:19
  • I tried it but its not working..text not moving but show still at the begging ??? – FriendOnline May 25 '12 at 05:23
  • This is not about dragging but..it should move automatically!! – FriendOnline May 25 '12 at 05:26
  • I don't know about perfect. Android does not clean the canvas every time it draws, it keeps painting over itself. So to avoid any dragging/blurring problems you need to draw over the canvas, or at least the area that was previously drawn on, with something. This can be an image, solid color, texture, whatever. – user11559 May 25 '12 at 05:28
  • Perhaps I don't understand your problem properly. Can you explain in more detail what the problem is and maybe link some images? – user11559 May 25 '12 at 05:30
  • I need to show that text to be move/motion some way..whatever direction no problem.. – FriendOnline May 25 '12 at 05:43
  • I tried running your code, and works as expected when i remove the line `setWillNotDraw(false);` – user11559 May 25 '12 at 06:00
  • Yes!! mate that's true its working fine without that..What does it actually mean?? setWillNotDraw(false); – FriendOnline May 25 '12 at 06:04
  • It is supposed to ensure the onDraw() method is called in an extended surfaceView. Apparently you should remove it if you override the onDraw() method. I don't really know anything about this myself: I was just reading this: http://stackoverflow.com/questions/2687015/extended-surfaceviews-ondraw-method-never-called – user11559 May 25 '12 at 06:10
  • How to execuete ondraw when touches the screen?? – FriendOnline May 25 '12 at 06:12
  • I need that drawing to be start whe touches the screen – FriendOnline May 25 '12 at 06:12
  • Use a boolean to control this. Change it to true when the screen is touched, and don't update your x and y values if the boolean is false. – user11559 May 25 '12 at 06:23
  • But I need that to be drawn very time it touches ?? – FriendOnline May 25 '12 at 06:29
  • You can set x and y to 0 when the user touches the screen. – user11559 May 25 '12 at 06:34