因此,我尝试创建像滚动视图一样的iMovie,当滚动视图内的图像触及白线时,我想获得一个回调(以便我可以更改大图像)。我怎样才能做到这一点?
该解决方案对我有用,这也是我对HorizontalScrollView的选项进行长期研究后唯一能够解决的问题。
ImageView lineImage =(ImageView)findViewById(R.id.yourImageViewIdOfWhiteLineImage);
然后在您的horizontalScrollView的onTouchListener中
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;
}
这严格按照您的要求。但我建议您在ACTION_UP情况下使用此代码,因为如果在ACTION_MOVE中执行所有这些计算,则此代码有使您的应用在滚动时变慢的趋势,并且,更多的计算会使应用变慢。因此,我建议在ACTION_UP中使用它。
您可以在两种情况下都测试您的应用,然后决定在哪种情况下需要此代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句