2014-09-02 47 views
0

我正在製作水平滾動視圖Galery,並且我想自動滾動它。現在它從左到右滾動,但當我到達列表的末尾時,我只是跳到第一個,但它看起來非常糟糕,所以我想要從開始滾動避免跳到第一個,或者如果它不可能當我到達右側的最後一個視圖(也許更好的選項)時,開始滾動到另一側。有人可以幫助我如何做到這一點?水平ScrollView Galery。滾動雙向或從開始

private LinearLayout horizontalOuterLayout; 
private HorizontalScrollView horizontalScrollview; 
private int scrollMax; 
private int scrollPos = 0; 
private TimerTask clickSchedule; 
private TimerTask scrollerSchedule; 
private TimerTask faceAnimationSchedule; 
private Timer scrollTimer = null; 
private Timer faceTimer = null; 
private String[] imageNameArray ={ "sponsors_czarnykot", "sponsors_estradarzeszow","sponsors_klubp","sponsors_kula","sponsors_czarnykot", "sponsors_estradarzeszow","sponsors_klubp","sponsors_kula" }; 


@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.horizontal_layout); 
    horizontalScrollview = (HorizontalScrollView) findViewById(R.id.horiztonal_scrollview_id); 
    horizontalOuterLayout = (LinearLayout) findViewById(R.id.horiztonal_outer_layout_id);  

    horizontalScrollview.setHorizontalScrollBarEnabled(false); 
    addImagesToView(); 

    ViewTreeObserver vto = horizontalOuterLayout.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() 
    { 
     @Override 
     public void onGlobalLayout() 
     { 
      horizontalOuterLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      getScrollMaxAmount(); 
      startAutoScrolling(); 
     } 
    }); 
} 

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() 
      { 
       moveScrollViewRight(); 
      } 
     }; 

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

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

/** Adds the images to view. */ 
public void addImagesToView() 
{ 
    for (int i = 0; i < imageNameArray.length; i++) 
    { 
     final ImageView imageView = new ImageView(this); 
     int imageResourceId = getResources().getIdentifier(imageNameArray[i], "drawable", getPackageName()); 
     Drawable image = this.getResources().getDrawable(imageResourceId); 
     imageView.setBackgroundDrawable(image); 
     imageView.setTag(i); 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(180, 123); 
     params.setMargins(0, 25, 0, 25); 
     imageView.setLayoutParams(params); 
     horizontalOuterLayout.addView(imageView); 
    } 
} 
public void stopAutoScrolling() 
{ 
    if (scrollTimer != null) 
    { 
     scrollTimer.cancel(); 
     scrollTimer = null; 
    } 
} 

public void onBackPressed() 
{ 
    super.onBackPressed(); 
    finish(); 
} 

public void onPause() 
{ 
    super.onPause(); 
    finish(); 
} 

public void onDestroy() 
{ 
    clearTimerTaks(clickSchedule); 
    clearTimerTaks(scrollerSchedule); 
    clearTimerTaks(faceAnimationSchedule); 
    clearTimers(scrollTimer); 
    clearTimers(faceTimer); 
    clickSchedule = null; 
    scrollerSchedule = null; 
    faceAnimationSchedule = null; 
    scrollTimer = null; 
    faceTimer = null; 
    super.onDestroy(); 
} 

private void clearTimers(Timer timer) 
{ 
    if (timer != null) 
    { 
     timer.cancel(); 
     timer = null; 
    } 
} 

private void clearTimerTaks(TimerTask timerTask) 
{ 
    if (timerTask != null) 
    { 
     timerTask.cancel(); 
     timerTask = null; 
    } 
} 

回答

0

以其他方式滾動回來將是最簡單的。

添加在被設定爲1.0的實例變量(稱爲說scrollDist

那麼這行

scrollPos = (int) (horizontalScrollview.getScrollX() + 1.0);

更改爲

scrollPos = (int) (horizontalScrollview.getScrollX() + scrollDist);

和這條線

scrollPos = 0;

scrollDist *= -1.0;

這樣,每次將扭轉它擊中了滾動的結束。

0

無休止的滾動使用以下snnipet

public void getScrollMaxAmount() { 
     int actualWidth = (horizontalOuterLayout.getMeasuredWidth() - getWindowManager().getDefaultDisplay().getWidth()); 
     scrollMax = actualWidth; 
    } 
public void moveScrollView() { 

     // ********************* Scrollable Speed *********************** 

     scrollPos = (int) (horizontalScrollview.getScrollX() + 1.0); 
     if (scrollPos >= scrollMax) { 
      Log.v("childCount", ""+scrollMax); 
      addImagesToView(); 
      getScrollMaxAmount(); 
     } 
     horizontalScrollview.scrollTo(scrollPos, 0); 
    }