在界限中绘制Canvas(Android)的麻烦

时间:2016-07-08 20:58:15

标签: java android canvas drawing mobile-application

我正在制作一个字母绘制应用程序,用户(通常是孩子)将能够填充应用程序中字母图像的颜色......

我制作的应用程序让我可以使用下面给出的代码在画布上绘制任何内容......

主要活动:

public class MainActivity extends Activity {
    SimpleDrawingView view;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        view = new SimpleDrawingView (this);

        setContentView(view);

                view.setBackgroundResource(R.drawable.a1);
    }
}

实际绘制视图的SimpleDrawingView类:

public class SimpleDrawingView extends View
 { 
    private final int paintColor = Color.BLACK;

    // defines paint and canvas
    private Paint drawPaint;

    // stores next circle
    private Path path = new Path();

    public SimpleDrawingView(Context context) { 
        super(context); 
        setFocusable(true); 
        setFocusableInTouchMode(true); 
        setupPaint();
    } 

    private void setupPaint() { 
        // Setup paint with color and stroke styles 
        drawPaint = new Paint(); 
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true); 
        drawPaint.setStrokeWidth(15);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND); 
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
    } 

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

    @Override public boolean onTouchEvent(MotionEvent event) {
        float pointX = event.getX();
        float pointY = event.getY(); 

        // Checks for the event that occurs

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: 
                path.moveTo(pointX, pointY);
                return true; 

            case MotionEvent.ACTION_MOVE:
                path.lineTo(pointX, pointY);
                break;

            default: return false; }

            // Force a view to draw again 

            postInvalidate(); 
            return true; 
        } 
}

它让我填写字母表,但它在整个屏幕/视图上绘制。我想让用户只填写/绘制字母而不是整个屏幕。

有人可以告诉我该怎么做或至少有一些指导方针来启动它吗?

0 个答案:

没有答案