【问题标题】:Display images in auto scrolling horizontalscrolview在自动滚动水平滚动视图中显示图像
【发布时间】:2014-02-12 11:24:12
【问题描述】:

我正在使用 Horizo​​ntalscrollView 将一组图像作为幻灯片移动。我有 5 张可绘制图像,我可以将图像从右向左移动,但在某些手机中它只是滚动到第三张图像并重新开始滚动,而在某些手机中它只是停止滚动直到到达右侧图像的末尾.请帮忙 !!这是我使用的代码:

 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);
    }

【问题讨论】:

    标签: android scrollview horizontalscrollview autoscroll


    【解决方案1】:
    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);
    }
    

    【讨论】:

    • 我试着用 512 代替,但它仍然不起作用.. 200 它正在滚动到最后一张图像并停在那里..
    猜你喜欢
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多