简体   繁体   中英

android multitouch on vogella tutorial not working

I tried this code of multitouch color dots given in tutorial http://www.vogella.com/tutorials/AndroidTouch/article.html

I want to place a dot on touch and when I move my finger one dot shuold remain there and another should move .just like joystick.

so popup dot appears and stays there until I lift my finger and if I move my finger another dot of same color should move

I tried this code

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);
    }

}

I also tried to store xx and yy in an array as xx[pointerID] and accessing that on canvas.drawCircle but application crashed

its code was like this

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);
    }

}

Please help.

crash log

06-01 16:48:42.781 10922-10922/com.example.nimishmaravikd.colourmtouch E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nimishmaravikd.colourmtouch, PID: 10922 java.lang.NullPointerException: Attempt to write to null array at com.example.nimishmaravikd.colourmtouch.MultitouchView.onTouchEvent(MultitouchView.java:73) at android.view.View.dispatchTouchEvent(View.java:9323) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2198) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2198) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2198) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2198) at com.android.internal.policy.PhoneWindow $DecorView.superDispatchTouchEvent(PhoneWindow.java:2405) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1739) at android.app.Activity.dispatchTouchEvent(Activity.java:2832) at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2366) at android.view.View.dispatchPointerEvent(View.java:9543) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4619) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4480) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4010) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4063) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4029) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4155) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4037) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4212) at android .view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4010) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4063) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4029) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4037) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4010) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6397) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6371) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6318) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6576) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:324) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5582) at java.lang.reflect.Method.invoke(Nat ive Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at 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

Don't change all the code:- As per my understanding you want to move same color.i just checked same color moves.

mPaint.setColor(colors[i % 1]);

Try this!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM