2

我想要一个滚动背景,如果我点击,如果我再次点击,滚动图像的方向会向左和向右改变。我希望它在我点击或触摸屏幕时继续图像的位置。我的代码所做的是每次我点击时,滚动背景的位置不会从我点击屏幕时的位置开始。请提前帮助和感谢

@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;
}
4

1 回答 1

1

您需要在切换方向后更新 bgrScroll,执行以下操作:

    case MotionEvent.ACTION_DOWN: {
        left = !left;
        bgrScroll = bgrW - bgrScroll;
        invalidate();
        break;
    }
于 2013-09-14T16:58:11.897 回答