所以我想創建像滾動視圖的iMovie,我想要在滾動視圖內的圖像觸摸白線時獲得回調(這樣我可以更改大圖像)。我怎樣才能做到這一點?如何檢測HorizontalScrollView內的視圖觸及另一個視圖?
2
A
回答
7
這個解決方案適用於我,也是經過長時間研究HorizontalScrollView的選項之後,我才能做出的唯一解決方案。
ImageView lineImage =(ImageView)findViewById(R.id.yourImageViewIdOfWhiteLineImage);
然後在你的horizontalScrollView
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.
隨着手指的移動,你會攔截它的位置的變化。我假設所有ImageView
s都在父容器中,並且水平滾動它們。如果您需要知道手指在上方懸停的孩子,請查看上一個問題: how to get a view from an event coordinates in android?。使用這些方法,您可以知道滾動哪個ImageView
與白線重疊,並相應地更新大圖像。
希望這會有所幫助!
相關問題
- 1. 如何在HorizontalScrollView中包裝視圖?
- 2. Objective-C:檢查視圖是否觸及另一個視圖
- 3. 的Android setOnClickListener()上HorizontalScrollView視圖
- 4. HorizontalScrollView中子視圖的位置
- 5. 如何檢測子視圖中的事件觸摸或如何在觸摸子視圖時觸及父視圖?
- 6. Android如何滾動HorizontalScrollView滾動另一個parellel HorizontalScrollView
- 7. Android horizontalscrollview。實用添加視圖
- 8. 檢測UIButton觸及子視圖
- 9. Android:檢測用戶是否觸及父視圖中的視圖
- 10. 如何檢測觸摸哪個視圖?
- 11. 在另一個horizontalscroll視圖中的水平滾動視圖
- 12. 在另一個子視圖頂部檢測一個子視圖
- 13. horizontalscrollview與圖像和文字可以添加兩個視圖
- 14. 檢測另一個視圖背後的視圖?
- 15. 如何在android中使用horizontalscrollview的可視區域截圖?
- 16. Android - HorizontalScrollView:如何滾動過去的視圖?
- 17. 從另一個視圖訪問另一個視圖的內容
- 18. 如何在HorizontalScrollView中使用自定義視圖?
- 19. 如何計算HorizontalScrollView長度(包括其子視圖)
- 20. 如何將自定義視圖添加到HorizontalScrollView?
- 21. 觸發從一個視圖到另一個視圖的事件
- 22. 檢測圖片框何時觸及另一個控件
- 23. 如何將數組從一個視圖傳遞到另一個視圖內的另一個視圖
- 24. 如何檢查視圖包含另一個視圖?
- 25. MPAndroidChart內HorizontalScrollView
- 26. 位圖ImageView,在HorizontalScrollView中。如何使HorizontalScrollView高度與位圖匹配?
- 27. Android-禁用水平滾動的horizontalscrollview作爲子視圖滾動視圖
- 28. HorizontalScrollView中的圖庫
- 29. 如何將觸摸事件從一個視圖傳遞到另一個視圖
- 30. ListView內的HorizontalScrollView
是你的問題解決了嗎?你可以發佈XML或Java代碼的'HorizontalScrollView'和白線。它會加快響應,因爲人們會得到一些代碼來嘗試,修改和使用。乾杯:) –
@Jimmy如果我沒有錯,你的白線或底部的電影帶以編程的方式移動,所以當這條白線出現在你想要舉起的另一個電影帶項目的上方時,你會用它來改變主體/更大的圖像)。對?如果我對你的要求錯誤,請糾正我。 –