2013-12-23 53 views
2

所以我想創建像滾動視圖的iMovie,我想要在滾動視圖內的圖像觸摸白線時獲得回調(這樣我可以更改大圖像)。我怎樣才能做到這一點?如何檢測Horizo​​ntalScrollView內的視圖觸及另一個視圖?

enter image description here

+0

是你的問題解決了嗎?你可以發佈XML或Java代碼的'Horizo​​ntalScrollView'和白線。它會加快響應,因爲人們會得到一些代碼來嘗試,修改和使用。乾杯:) –

+0

@Jimmy如果我沒有錯,你的白線或底部的電影帶以編程的方式移動,所以當這條白線出現在你想要舉起的另一個電影帶項目的上方時,你會用它來改變主體/更大的圖像)。對?如果我對你的要求錯誤,請糾正我。 –

回答

7

這個解決方案適用於我,也是經過長時間研究Horizo​​ntalScrollView的選項之後,我才能做出的唯一解決方案。

ImageView lineImage =(ImageView)findViewById(R.id.yourImageViewIdOfWhiteLineImage);

然後在你的horizo​​ntalScrollView

public boolean onTouch(View v, MotionEvent event) { 
// TODO Auto-generated method stub 
switch (event.getAction()){ 
    case MotionEvent.ACTION_MOVE: 
    //get horizontal scroll view for images 
    HorizontalScrollView parent=(HorizontalScrollView)v; 
    //get linear layout for list of images 
    LinearLayout wholeList=(LinearLayout)parent.getChildAt(0); 
    int centerCord[] = new int[2]; 
    for(int idx=0;idx<wholeList.getChildCount();idx++){ 
     ImageView discoveredIv=(ImageView)wholeList.getChildAt(idx); 
     discoveredTv.getLocationInWindow(centerCord); 
     int discoveredX=centerCord[0]; 
     //set these (lineImage.getLeft()-20, lineImage.getLeft()+20) according to your 
     //requirement, the way it best suits you. You'll have to test in runtime, how your 
     //views come under the line and then set these. 
     if(discoveredX >= lineImage.getLeft()-20 && discoveredX <= lineImage.getLeft()+20) 
     { 
      //you have a winner, take that image from imageView and set it wherever you want. 
      //Your image is in discoveredIv 
     } 
    } 
     return false; 
    case MotionEvent.ACTION_UP: 
     //It returns true to avoid fling effect, otherwise this method won't work. 
     return true; 
    } 
return false; 
} 

的onTouchListener這是嚴格按照您的要求。但是我建議你在ACTION_UP的情況下使用這段代碼,因爲如果你在ACTION_MOVE中做了所有這些計算,那麼這段代碼就有在滾動時讓你的應用程序變慢的趨勢,而且計算越多,應用程序的運行速度就越慢。 所以,我建議在ACTION_UP中使用它。

您可以在兩種情況下測試您的應用程序並決定在哪種情況下您需要此代碼。

1

設置OnTouchListener爲ImageView的,則u已經實現下面的回調。其中,事件引用將給予一切什麼ü需要

public boolean onTouch(View v, MotionEvent event) { 
// TODO Auto-generated method stub 
// event reference will give every co-ordinate that u need 
return false; 
} 
1

你可能會想以擴展該屏幕上報告其位置滾動,因爲它是被感動了家長。我不確定從屏幕截圖看,如果一行,一卷圖像或兩者都滾動,所以我將假定白線是固定的,並且圖像滾動(儘管您的解決方案可以相應地進行調整)。

在這種滾動ImageViews的自定義視圖:

/* @return True if the event was handled, false otherwise. */ 
public boolean onTouchEvent (MotionEvent event) { 
    switch(event.getActionMasked()) { 
     case MotionEvent.ACTION_MOVE: { 
      x = event.getRawX(); 
      // This is the x-position on screen, compare to that of while line. 

隨着手指的移動,你會攔截它的位置的變化。我假設所有ImageViews都在父容器中,並且水平滾動它們。如果您需要知道手指在上方懸停的孩子,請查看上一個問題: how to get a view from an event coordinates in android?。使用這些方法,您可以知道滾動哪個ImageView與白線重疊,並相應地更新大圖像。

希望這會有所幫助!

相關問題