0

我一直在开发一个应用程序来绘制 ics android 平板电脑,我遇到了一个我不知道如何解决的问题。

问题是我绘制正确并且实时绘制但是当我走得非常快(在真正的平板电脑上测试)时,圆圈并不是真正的圆圈,它们看起来像 5 或 6 边的 pilygons ......

在这里,我声明位图并将其分配给画布:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    width = size.x;
    height = size.y;
    bm = Bitmap.createBitmap(width, height-50,Bitmap.Config.ARGB_8888); 
    c = new Canvas(bm);

这里是我用来获取 x, y 的代码:(layP 是“Painter”,你将在这里看到

class SaveOnTouchListener implements OnTouchListener{

    public boolean onTouch(View v, MotionEvent e) {
        final float x = e.getX();
        final float y = e.getY();
        if(e.getAction() == MotionEvent.ACTION_DOWN){  
            startx.add(x);
            starty.add(y);
            x1 = x;
            y1 = y;
        } else if(e.getAction() == MotionEvent.ACTION_MOVE){  
            endx.add(x);
            endy.add(y);
            x2 = x;
            y2 = y;
            if (id == 1) strokes.add(sb.getProgress()+1);
            else strokes.add(4*(sb.getProgress()+1));
            layP.draw();
            startx.add(x);
            starty.add(y);
            x1 = x;
            y1 = y;
        } else if(e.getAction() == MotionEvent.ACTION_UP){  
            x2 = x;
            y2 = y;
            endx.add(x);
            endy.add(y);
            strokes.add(stroke);
            layP.draw();
            return false;
        }
        return true;
    }

}

最后是“画家”的代码,它是画布和 onDraw() 方法(我使用 invalidate(t,l,r,b) 对其进行优化......)

private class Painter extends View{
    public Painter(Context context){
        super(context);
    }

    public void draw() {
        if (firstPainting) {
                            //paint the canvas white just once
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            c.drawPaint(paint);
            firstPainting = false;
            layP.invalidate();
        }
        else {
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                paint.setAntiAlias(true);
                int r = startx.get(startx.size()-1).intValue();
                int t = starty.get(starty.size()-1).intValue();
                int l = endx.get(endx.size()-1).intValue();
                int b = endy.get(endy.size()-1).intValue();
                int n = strokes.get(strokes.size()-1);
                paint.setStrokeWidth(n);
                paint.setColor(COLOR.BLACK);
                c.drawLine(r, t, l, b, paint);
                c.drawCircle(r, t, n/2, paint);

                if (l > r) {
                    int aux = l;
                    l = r;
                    r = aux;
                }
                if (t > b) {
                    int aux = t;
                    t = b;
                    b = aux;
                }
                r += n;
                l -= n;
                t -= n;
                b += n;
                if (t < 0) t = 0;
                if (l < 0) l = 0;
                layP.invalidate(l,t,r,b);
        }
    }


    @Override 
    protected void onDraw(Canvas c) {
            c.drawBitmap(bm, 0, 0, null);
    }
}
}

正如你所看到的,我使用了一个位图,我只是使需要失效的区域无效,我不知道还能做什么。

有没有正确绘制的方法?即使我必须更改所有位图和画布..

我试图实现贝塞尔,但我不知道该怎么做,因为在画线之前我需要下一点。

我找到了贝塞尔方法,我想我要试试这个。

4

1 回答 1

1

我根本不清楚你想做什么。从您的文字中,听起来您正在尝试进行基本的“手指绘画”,您基本上只是绘制与输入匹配的曲线。您的代码对我来说看起来不太像,但是在您使用的变量之间,我们没有看到 (strokestrokes) 的声明和单字母变量名称,我有点迷茫。当然是这两行:

c.drawLine(r, t, l, b, paint);
c.drawCircle(r, t, n/2, paint);

让您看起来不仅仅是简单地绘制用户用手指绘制的内容。

Still, I'd like to try to help. drawCircle() really should always draw circles. Drawing lines between points is going to give you sharp corners (polygons if you close a shape). If you want to draw paths like this smoothly, then Bézier curves are indeed what you want.

If you download the sample code from the SDK, there's an example called "FingerPaint" that does a very good job demonstrating how to draw arbitrary curves smoothly.

于 2012-07-06T03:47:28.653 回答