canvas scrolling background
i want a scrolling background wherein if i tap, the direction of the
scrolling image changes to left and right if i tap again. I want it to
continue the position of the image when i tap or touch the screen. What my
code does is everytime i tap, the position of the scrolling background
doesnt begin on where it was when i tapped the screen. please help and
thanks in advance
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
fromRect1 = new Rect(0, 0, bgrW - bgrScroll, bgrH);
toRect1 = new Rect(bgrScroll, 0, bgrW, bgrH);
fromRect2 = new Rect(bgrW - bgrScroll, 0, bgrW, bgrH);
toRect2 = new Rect(0, 0, bgrScroll, bgrH);
if(left == true){
if (!reverseBackroundFirst) {
canvas.drawBitmap(bgr, toRect1, fromRect1, null);
canvas.drawBitmap(bgrReverse, toRect2, fromRect2, null);
}
else{
canvas.drawBitmap(bgr, toRect2, fromRect2, null);
canvas.drawBitmap(bgrReverse, toRect1, fromRect1, null);
}
}else{
if (!reverseBackroundFirst) {
canvas.drawBitmap(bgr, fromRect1, toRect1, null);
canvas.drawBitmap(bgrReverse, fromRect2, toRect2, null);
}
else{
canvas.drawBitmap(bgr, fromRect2, toRect2, null);
canvas.drawBitmap(bgrReverse, fromRect1, toRect1, null);
}
}
if ( (bgrScroll += dBgrY) >= bgrW) {
bgrScroll = 0;
reverseBackroundFirst = !reverseBackroundFirst;
}
}
//***************************************
//************* TOUCH *****************
//***************************************
@Override
public synchronized boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
left = !left;
invalidate();
break;
}
case MotionEvent.ACTION_MOVE: {
break;
}
case MotionEvent.ACTION_UP:
break;
}
return true;
}
No comments:
Post a Comment