Reputation: 51
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;
}
Upvotes: 2
Views: 675
Reputation: 1436
You need to update bgrScroll after switching directions, do this:
case MotionEvent.ACTION_DOWN: {
left = !left;
bgrScroll = bgrW - bgrScroll;
invalidate();
break;
}
Upvotes: 1