在Android的单个屏幕上使用画布绘制多个形状?

时间:2014-02-14 18:36:25

标签: android

我正在开发一个类似于paint的应用程序,其中包含Line circle等工具。

目前我可以在画布上一次绘制一个形状 现在,我想使用绘制线,绘制圆等按钮。 所有这些变化都应该在同一个画布上完成。

任何帮助将不胜感激

public class StraightLine extends View implements OnTouchListener {
private static final String TAG = "DrawView";

private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;

private Canvas mCanvas;
private Path mPath;
private Paint mPaint;
private LinkedList<Path> paths = new LinkedList<Path>();
private int mWidth;
private int mHeight;

public StraightLine(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);


    this.setOnTouchListener(this);

    mPaint = new Paint();
    mPaint.setAlpha(0x80);
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.BUTT);
    mPaint.setStrokeWidth(5);
    mCanvas = new Canvas();
    mPath = new Path();
    paths.add(mPath);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {

    Bitmap mapImg = BitmapFactory.decodeResource(getResources(),
            R.drawable.notepad);
    canvas.drawBitmap(mapImg, 0, 0, null);
    for (Path p : paths) {
        canvas.drawPath(p, mPaint);
    }
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {/*
                                                         * mPath.quadTo(mX,
                                                         * mY, (x + mX)/2,
                                                         * (y + mY)/2); mX =
                                                         * x; mY = y;
                                                         */
    }

    mX = x;
    mY = y;

}

private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath = new Path();
    paths.add(mPath);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
    mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(mWidth, mHeight);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    v.
    PointF startPoint = new PointF(event.getX(), event.getY());
    PointF endPoint = new PointF();

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:

        touch_start(x, y);
        invalidate();

        break;
    case MotionEvent.ACTION_MOVE:
        /*
         * float dx = Math.abs(x - mX); System.out.println("action move");
         * float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >=
         * TOUCH_TOLERANCE) { // currentDrawingPath.path.quadTo(mX,mY,(x +
         * mX)/2, (y + mY)/2); } mX = x; mY = y;
         */

        touch_move(x, y);
        endPoint.x = event.getX();
        endPoint.y = event.getY();
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        mPath.lineTo(mX, mY);
        /*
         * float deltaX = endPoint.x - startPoint.x; float deltaY =
         * endPoint.y - startPoint.y; float frac = (float) 0.1;
         * 
         * float point_x_1 = startPoint.x + (float) ((1 - frac) * deltaX +
         * frac * deltaY); float point_y_1 = startPoint.y + (float) ((1 -
         * frac) * deltaY - frac * deltaX);
         * 
         * float point_x_2 = endPoint.x; float point_y_2 = endPoint.y;
         * 
         * float point_x_3 = startPoint.x + (float) ((1 - frac) * deltaX -
         * frac * deltaY); float point_y_3 = startPoint.y + (float) ((1 -
         * frac) * deltaY + frac * deltaX);
         * 
         * mPath.moveTo(point_x_1, point_y_1); mPath.lineTo(point_x_2,
         * point_y_2); mPath.lineTo(point_x_3, point_y_3);
         * mPath.lineTo(point_x_1, point_y_1); mPath.lineTo(point_x_1,
         * point_y_1);
         */mCanvas.drawPath(mPath, mPaint);
        mPath = new Path();
        paths.add(mPath);

        invalidate();
        break;

    }
    return true;
}

}

上面的代码在画布上绘制线条。同样,我也有圆形类..所以我怎样才能让这两个类在单个画布上绘制?

0 个答案:

没有答案