0

我在android中制作了一个简单的多点触控程序用于学习目的,我尝试过如下但在触摸模拟器上没有画线..所以请帮助我..我的代码如下:

多点触控.java

package com.example.multitouch;

import java.security.InvalidAlgorithmParameterException;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MultiTOuchEvent extends View {
    private Paint paint = new Paint();
    private Path path = new Path();

    public MultiTOuchEvent(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {


        int action = event.getAction();
        if (event.getPointerCount()>1){
            int actionEvent = action & MotionEvent.ACTION_MASK;
            int actionPointerId= action & MotionEvent.ACTION_POINTER_INDEX_MASK;

            // Find the index of the pointer
            int index = event.findPointerIndex(actionPointerId);

            // Gets its coordinates
            float eventX = event.getX(index);
            float eventY = event.getY(index);

            // TODO Do something with the data

        }

        // Schedules a repaint.
        invalidate();
        return true;
    }
}

main.java

package com.example.multitouch;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MultiTOuchEvent(this, null));
    }

}
4

0 回答 0