0

我使用horizo​​ntalscrollView连同动画来移动一组图像作为幻灯片。我可以使用以下代码将图像从右向左移动:

 public void getScrollMaxAmount(){
    int actualWidth = (horizontalOuterLayout.getMeasuredWidth()-512);
    scrollMax   = actualWidth;
}

public void startAutoScrolling(){
    if (scrollTimer == null) {
        scrollTimer =   new Timer();
        final Runnable Timer_Tick = new Runnable() {
            public void run() {
                moveScrollView();
            }
        };

        if(scrollerSchedule != null){
            scrollerSchedule.cancel();
            scrollerSchedule = null;
        }
        scrollerSchedule = new TimerTask(){
            @Override
            public void run(){
                runOnUiThread(Timer_Tick);
            }
        };

        scrollTimer.schedule(scrollerSchedule, 30, 30);
    }
}

public void moveScrollView(){
    scrollPos   =   (int) (horizontalScrollview.getScrollX() + 1.0);
    if(scrollPos >= scrollMax){
        scrollPos = 0;
    }
    horizontalScrollview.scrollTo(scrollPos, 0);

}

我现在想将图像从右到左移动为幻灯片。我找不到正确的公式/逻辑。请帮助我。:(

4

1 回答 1

2

在启动计时器之前,请设置:

scrollPos= scrollMax;

然后使用这个 moveScrollView 函数:

public void moveScrollView(){
    scrollPos   =   (int) (horizontalScrollview.getScrollX() - 1.0);
    if(scrollPos <= 0){
        scrollPos = scrollMax;
    }
    horizontalScrollview.scrollTo(scrollPos, 0);
}

用于计算水平滚动视图内容的宽度:

protected void onCreate(Bundle savedInstanceState) 
{
        ...

    horizontalScrollview= (HorizontalScrollView) findViewById(R.id.hsv);
    ViewTreeObserver vto = horizontalScrollview.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            horizontalScrollview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            scrollMax= horizontalScrollview.getChildAt(0).getMeasuredWidth()-getWindowManager().getDefaultDisplay().getWidth();

        }
    });     
}
于 2012-03-08T09:02:23.007 回答