Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在自动滚动水平视图中显示图像_Android_Scrollview_Horizontalscrollview_Autoscroll - Fatal编程技术网

Android 在自动滚动水平视图中显示图像

Android 在自动滚动水平视图中显示图像,android,scrollview,horizontalscrollview,autoscroll,Android,Scrollview,Horizontalscrollview,Autoscroll,我正在使用horizontalscrollView以幻灯片形式移动一组图像。我在drawable中有5个图像,我可以从右向左移动图像,但在一些手机中,它只是滚动到第3个图像并重新启动滚动,在一些手机中,它只是停止滚动,直到到达右侧图像的末尾。请帮忙!!这是我使用的代码: public void getScrollMaxAmount(){ int actualWidth = (horizontalOuterLayout.getMeasuredWidth()-200); scrol

我正在使用horizontalscrollView以幻灯片形式移动一组图像。我在drawable中有5个图像,我可以从右向左移动图像,但在一些手机中,它只是滚动到第3个图像并重新启动滚动,在一些手机中,它只是停止滚动,直到到达右侧图像的末尾。请帮忙!!这是我使用的代码:

 public void getScrollMaxAmount(){
    int actualWidth = (horizontalOuterLayout.getMeasuredWidth()-200);
    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, 50, 50);
    }
}

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

public void addImagesToView(){
    for (int i=0;i<imageNameArray.length;i++){
        Log.d("imagname",String.valueOf(imageNameArray.length));
        final ImageView imageButton =   new ImageView(this);
    int imageResourceId      =  getResources().getIdentifier(imageNameArray[i], "drawable",getPackageName());
     Drawable image  =  this.getResources().getDrawable(imageResourceId);

        imageButton.setBackgroundDrawable(image);
        imageButton.setTag(i);
LinearLayout.LayoutParams params    =   new LinearLayout.LayoutParams(90,LayoutParams.WRAP_CONTENT);
        //params.setMargins(0, 25, 0, 25);
        imageButton.setLayoutParams(params);
        horizontalOuterLayout.addView(imageButton);
    }

我试着换了512,但还是不行。。有了200,它将滚动到最后一个图像并停在那里。。
First calculate width of your scrollview like this here linearlayout is layout inside your scrollview 


    linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    LayoutParams layParamsGet = linearLayout.getLayoutParams();
                    width = layParamsGet.width;

                }
            });


Then in timertassk just scroll the scrollview upto end

private void callAsynchronousTask() {
    final Handler handler = new Handler();
    timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {

                        if (x <= width - KeyConstants.SCREEN_WIDTH) {
                            x = x + 1;
                        } else {

                            x = 0;
                        }

                        mhorizontalscrollView.scrollTo(x, 0);

                    } catch (Exception e) {
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 5);
}