how to act over two SeekBar instances (or any other control, e.g. Button) simultaneously using touchscreen?
I'm developing an app that will control the volume of up to eight music tracks simultaneously. One use-case is the dj action of mixing two tracks by fading-out Track1 and fading-in Track2 at the same time.
Given the above, I开发者_开发问答 created an Activity with two SeekBar instances and onProgressChanged(...) event implemented. Now, my issue arises! I cannot control the two SeekBars simultaneously. I can only act over one at a time.
Looking at multi-touch documentation for Android platform, I can only get frightened by its complexity. And looking at the code makes me believe the solutions does not lie, at least directly, there.
Any ideas?
Thanks, PP
OK. So, after further research on how to handle multi-touch events and following Damian's tip, I've got to the point where I intercept all touch events in order to determine if each pointer (in this case, a finger) is sliding on a set of pre-determined SeekBar instances, vol1 and vol2.
Here's goes the code to put on your Activity:
@Override
public boolean dispatchTouchEvent (MotionEvent ev)
{
Rect sb1Rect = new Rect();
Rect sb2Rect = new Rect();
vol1.getGlobalVisibleRect(sb1Rect);
vol2.getGlobalVisibleRect(sb2Rect);
for (int i = 0; i < ev.getPointerCount(); i++) {
int pId = ev.getPointerId(i);
int coordX = (int) ev.getX(i);
int coordY = (int) ev.getY(i);
if(sb1Rect.contains(coordX, coordY)) {
Log.d(TAG, "[PID=" + pId + "] -> SeekBar 1 -> x:" + coordX + ", y:" + coordY);
} else if (sb2Rect.contains(coordX, coordY)) {
Log.d(TAG, "[PID=" + pId + "] -> SeekBar 2 -> x:" + coordX + ", y:" + coordY);
}
}
LinearLayout l =(LinearLayout) this.findViewById(R.id.linearLayout1);
l.dispatchTouchEvent(ev);
return true;
}
Now, I need only to calculate the value of each touched SeekBar based on its relative X coordinate (not the absolute which is related to the entire Activity) and set the progress value accordingly (.setProgress(int value)).
Build a parent/layout which will dispatch multi-touch events as a separated events to appropriate children.
//EDIT http://androiddev.vipserv.org/wordpress/?p=112
精彩评论