如何在CanvasView中禁用用户触摸事件?

时间:2018-08-17 23:39:02

标签: android android-canvas

我个人已经实现了画布生成类,我需要在特定时间内通过x和y坐标绘制图形。我能够正常执行绘图,但是我需要使视图不接受用户的触摸,即仅自动绘制绘图,而无需用户交互。

以下是我已经实现的一些重要的类和方法:

public class CanvasView extends View {

public int width;
public int height;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
Context mContext;
private Paint mPaint;
private float mX, mY;
private static final float TOLERANCE = 5;


public CanvasView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;

    // we set a new Path
    mPath = new Path();
    // and we set a new Paint with the desired attributes
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeWidth(20f);
    mPaint.setPathEffect(new DashPathEffect(new float[]{50, 20}, 0));
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    // your Canvas will draw onto the defined Bitmap
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // draw the mPath with the mPaint on the canvas when onDraw
    canvas.drawPath(mPath, mPaint);
}

// when ACTION_DOWN start touch according to the x,y values
private void startTouch(float x, float y) {
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

// when ACTION_MOVE move touch according to the x,y values
private void moveTouch(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOLERANCE || dy >= TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

public void clearCanvas() {
    mPath.reset();
    invalidate();
}

// when ACTION_UP stop touch
private void upTouch() {
    mPath.lineTo(mX, mY);
}

//override the onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startTouch(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            moveTouch(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            upTouch();
            invalidate();
            break;
    }
    return false;
}

}

我主要活动的一些方法:

public void shootEventTouch(int motionEventType, float coordinateX, float coordinateY) {
    // Obtain MotionEvent object
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis() + 100;
    float x = coordinateX;
    float y = coordinateY;
    // List of meta states found here:     developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    int metaState = 0;
    MotionEvent motionEvent = MotionEvent.obtain(
            downTime,
            eventTime,
            motionEventType,
            x,
            y,
            metaState
    );
    // Dispatch touch event to view
    //canvasView.dispatchTouchEvent(motionEvent);
    canvasView.onTouchEvent(motionEvent);
}

public void startDrawing() {
    readFilesCoordinates();
    final int size = (listCoordinateX.size() <= listCoordinateY.size()) ? listCoordinateX.size() : listCoordinateY.size();
    shootEventTouch(MotionEvent.ACTION_DOWN, listCoordinateX.get(0) - 1, listCoordinateY.get(0) - 1);
    CountDownTimer timer = new CountDownTimer(size * 200, 200) {
        private int i = 0;
        @Override
        public void onTick(long millisUntilFinished) {
            shootEventTouch(MotionEvent.ACTION_MOVE, listCoordinateX.get(i), listCoordinateY.get(i));
            i++;
        }
        @Override
        public void onFinish() {
            shootEventTouch(MotionEvent.ACTION_DOWN, listCoordinateX.get(listCoordinateX.size()-1) + 1, listCoordinateY.get(listCoordinateY.size()-1) + 1);

        }
    };
    timer.start();
}

“ readFilesCoordinates()”方法负责读取资产中我文件的x和y坐标。

几秒钟后不触摸屏幕的结果是: MainActivity

0 个答案:

没有答案