So I'm trying to create iMovie like scroll view, I want to get a callback when the image inside the scroll view touch the white line (so I could change the large image). How can I do that?
-
is your problem resolved yet? Could you post XML or java code for the `HorizontalScrollView` and the white line. It would speed up the response as people will get some code to try, modify and work with. Cheers :) – Amulya Khare Jan 01 '14 at 07:13
-
@Jimmy If I am not wrong your white line or bottom film strip moves programmatically, so when when this white line comes on top of another film strip item you want to raise and event (which will you use to change the main/bigger image). Right? Correct me in case I am wrong about your requirement. – Tabrej Khan Jan 01 '14 at 10:37
3 Answers
This solution works for me and also this is the only solution I was able to make out after a long research on options we have with HorizontalScrollView.
ImageView lineImage=(ImageView)findViewById(R.id.yourImageViewIdOfWhiteLineImage);
Then in onTouchListener of your 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;
}
This is strictly according to your requirement. But I'd suggest you use this code in ACTION_UP case, because if you do all these calculations in ACTION_MOVE then this code has tendency to make your app slow while scrolling and also, more the calculations more the app slows down. so, i suggest to use this in ACTION_UP.
You can test your app in both cases and decide in which case you want this code.

- 1,527
- 4
- 20
- 42
Set OnTouchListener for imageview, then u ve to implement below callback. where event reference will give everything what u need
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
// event reference will give every co-ordinate that u need
return false;
}

- 3,864
- 2
- 17
- 35
You are probably going to want to extend the parent that scrolls that reports its location on screen as it is being touched. I'm not sure from your screenshot if the while line, the image reel, or both scroll, so I am going to assume the white line is fixed and the images scroll (although your solution could be adapted accordingly).
In a custom view that scrolls the 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.
As the finger moves, you will intercept it's change in position. I assume all ImageView
s are in a parent container that scrolls them horizontally. If you need to know which child the finger is hovering above, take a look at at the previous question:
how to get a view from an event coordinates in android?. Using these method you can know as you scroll which ImageView
is overlapping with the white line, and update the large image accordingly.
Hope this helps!