Vishwas Shukla
Vishwas Shukla

Reputation: 29

Looping a canvas in Android

Does anyone know how to loop this drawing of the Canvas? I want to later on add an onTouch method and get the X position. Thank you.

  @Override
protected void onDraw(Canvas canvas) {
    //Canvas canvas= new Canvas();
    xp1 = canvas.getWidth()/2;
    Log.d("test1", "It went pass onDraw");
    xp2 = canvas.getWidth()/2;
    yp1 = 25;
    yp2 = 760;
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(paddle1, xp1,yp1, null);
    canvas.drawBitmap(paddle2,xp2,yp2, null);
    Paint white = new Paint();
    white.setColor(Color.WHITE);
    canvas.drawText("Score P1:"+ p1Score +" P2: " + p2Score , 700, 20,white );  
    Log.d("test1", "It's done with onDraw");

}

Upvotes: 0

Views: 263

Answers (1)

jeet.chanchawat
jeet.chanchawat

Reputation: 2575

call method

invalidate();

whenever you want to make a callback to 'onDraw' method

aSampleCode(){
        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch(event.getAction())
                {
                case MotionEvent.ACTION_MOVE:

                    x= event.getRawX()

                    break;
                case MotionEvent.ACTION_UP:
                                    invalidate();
                    break;
                case MotionEvent.ACTION_DOWN:
                    break;

                default:
                    break;
                }
                return true;
            }
        });
    }
}

Upvotes: 1

Related Questions