我尝试了教程中给出的多点触控彩色点代码 http://www.vogella.com/tutorials/AndroidTouch/article.html
我想在触摸时放置一个点,当我移动手指时,一个点shuold保持在那里而另一个应该移动。就像操纵杆一样。
所以出现弹出点并保持在那里,直到我抬起手指,如果我移动手指,另一个相同颜色的点应移动
我试过这段代码
public class MultitouchView extends View {
private static final int SIZE = 150;
float xx;
float yy;
float[] abcisa;
float[] ordinate;
private SparseArray<PointF> mActivePointers;
private Paint mPaint;
private int[] colors = { Color.BLUE, Color.GREEN, Color.MAGENTA,
Color.BLACK, Color.CYAN, Color.GRAY, Color.RED, Color.DKGRAY,
Color.LTGRAY, Color.YELLOW };
private Paint textPaint;
public MultitouchView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private void initView() {
mActivePointers = new SparseArray<PointF>();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
// set painter color to a color you like
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(20);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// get pointer index from the event object
int pointerIndex = event.getActionIndex();
// get pointer ID
int pointerId = event.getPointerId(pointerIndex);
// get masked (not specific to a pointer) action
int maskedAction = event.getActionMasked();
switch (maskedAction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN: {
// We have a new pointer. Lets add it to the list of pointers
PointF f = new PointF();
xx = f.x = event.getX(pointerIndex);
yy = f.y = event.getY(pointerIndex);
canvas.drawCircle(xx, yy, SIZE, mPaint);
mActivePointers.put(pointerId, f);
break;
}
case MotionEvent.ACTION_MOVE: { // a pointer was moved
for (int size = event.getPointerCount(), i = 0; i < size; i++) {
PointF point = mActivePointers.get(event.getPointerId(i));
if (point != null) {
point.x = event.getX(i);
point.y = event.getY(i);
}
}
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL: {
mActivePointers.remove(pointerId);
break;
}
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// draw all pointers
for (int size = mActivePointers.size(), i = 0; i < size; i++) {
PointF point = mActivePointers.valueAt(i);
if (point != null)
mPaint.setColor(colors[i % 9]);
canvas.drawCircle(xx, yy, SIZE, mPaint);
canvas.drawCircle(point.x, point.y, SIZE, mPaint);
}
canvas.drawText("Total pointers: " + mActivePointers.size(), 10, 40 , textPaint);
}
}
我还尝试将xx和yy作为xx [pointerID]存储在数组中,并在canvas.drawCircle上访问它,但是应用程序崩溃了
它的代码就像这样
public class MultitouchView extends View {
private static final int SIZE = 150;
float[] xx;
float[] yy;
private SparseArray<PointF> mActivePointers;
private Paint mPaint;
private int[] colors = { Color.BLUE, Color.GREEN, Color.MAGENTA,
Color.BLACK, Color.CYAN, Color.GRAY, Color.RED, Color.DKGRAY,
Color.LTGRAY, Color.YELLOW };
private Paint textPaint;
public MultitouchView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private void initView() {
mActivePointers = new SparseArray<PointF>();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
// set painter color to a color you like
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(20);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// get pointer index from the event object
int pointerIndex = event.getActionIndex();
// get pointer ID
int pointerId = event.getPointerId(pointerIndex);
// get masked (not specific to a pointer) action
int maskedAction = event.getActionMasked();
switch (maskedAction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN: {
// We have a new pointer. Lets add it to the list of pointers
PointF f = new PointF();
xx[pointerId] = f.x = event.getX(pointerIndex);
yy[pointerId] = f.y = event.getY(pointerIndex);
mActivePointers.put(pointerId, f);
break;
}
case MotionEvent.ACTION_MOVE: { // a pointer was moved
for (int size = event.getPointerCount(), i = 0; i < size; i++) {
PointF point = mActivePointers.get(event.getPointerId(i));
if (point != null) {
point.x = event.getX(i);
point.y = event.getY(i);
}
}
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL: {
mActivePointers.remove(pointerId);
break;
}
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// draw all pointers
for (int size = mActivePointers.size(), i = 0; i < size; i++) {
PointF point = mActivePointers.valueAt(i);
if (point != null)
mPaint.setColor(colors[i % 9]);
canvas.drawCircle(xx[i], yy[i], SIZE, mPaint);
canvas.drawCircle(point.x, point.y, SIZE, mPaint);
}
canvas.drawText("Total pointers: " + mActivePointers.size(), 10, 40 , textPaint);
}
}
请帮忙。
崩溃日志
06-01 16:48:42.781 10922-10922 / com.example.nimishmaravikd.colourmtouch E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.nimishmaravikd.colourmtouch,PID:10922 java.lang.NullPointerException:尝试写入null数组 在com.example.nimishmaravikd.colourmtouch.MultitouchView.onTouchEvent(MultitouchView.java:73) 在android.view.View.dispatchTouchEvent(View.java:9323) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2198) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2198) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2198) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2198) 在com.android.internal.policy.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2405) 在com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1739) 在android.app.Activity.dispatchTouchEvent(Activity.java:2832) 在com.android.internal.policy.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2366) 在android.view.View.dispatchPointerEvent(View.java:9543) 在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4619) 在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4480) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4010) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4063) 在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4029) 在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4155) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4037) 在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4212) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4010) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4063) 在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4029) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4037) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4010) 在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6397) 在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6371) 在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6318) 在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6576) 在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 在android.os.MessageQueue.nativePollOnce(本机方法) 在android.os.MessageQueue.next(MessageQueue.java:324) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5582) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-01 16:48:42.791 10922-10933 / com.example.nimishmaravikd.colourmtouch I / ActivityThreadEui:schedulePauseActivity com.example.nimishmaravikd.colourmtouch.MainActivity finished = true userLeaving = false configChanges = 0 dontReport = false
答案 0 :(得分:0)
不要更改所有代码: - 根据我的理解,你想要移动相同的颜色。我只是检查相同的颜色移动。
mPaint.setColor(colors[i % 1]);
试试这个!!