在画布上的位图的Andr​​oid芯片快速绘制 [英] Drawing fast in canvas Bitmap android ics

查看:154
本文介绍了在画布上的位图的Andr​​oid芯片快速绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个应用程序中的IC Android平板电脑绘制和我遇到一个问题,我无法弄清楚如何解决。

I've been developing an app to draw in ics android tablets and i've encountered an issue I can't figure out how to fix.

问题是,我画正确,并在实时绘制,但是当我去真的快(真实测试片)的圆圈是不是真的圈,它们看起来像5或6两侧pilygons ...

The problem is that I draw correctly and in a real time drawing BUT when I go really really fast (tested on real tablets) the circles are not really circles, they look like pilygons of 5 or 6 sides...

下面我宣布位图,并将其分配到画布:

Here I declare the bitmap and assign it to the canvas:

    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);

在这里,code我用得到的X,Y:(该layP是刷,你会本身到这里

Here the code I use to get the x, y: (the layP is the "Painter" which you'll se down here

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;
    }

}

最后,这里的code,为画家,这是画布和的onDraw()方法(我用的无效(T,L,R,B)做了优化....)

And finally here's the code for the "painter" which is the canvas and the onDraw() method (i use the invalidate(t,l,r,b) to do it optimized....)

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);
    }
}
}

正如你可以看到我使用位图,我只是失效需要无效的区域,我不知道自己还能做些什么。

As you can see I use a BitMap and I just invalidate the zone needed to be invalidated, I don't know what else to do.

有没有办法正确地画?即使我不得不改变所有的位图和帆布..

Is there a way to draw properly? Even if I have to change all the bitmap and canvas..

我试图实现贝塞尔曲线,但我不知道该怎么做它,因为我绘制行之前需要下一个点。

I've tried to to implement Bezier but I don't know how to do it since I need the next points before drawing the lines.

我已经找到了贝塞尔方法,我想我会尝试了这一点。

I've found the bezier method, I think I'm going to try this out.

推荐答案

这不是在所有我清楚你想要做什么。从你的文字,这听起来像你正在试图做基本的手指画,在这里你基本上只画匹配输入的曲线。您code不看很像我,但变量之间你使用,我们还没有看到声明(行程笔画)和单字母的变量名,我有点失落。当然,这两行:

It's not at all clear to me what you're trying to do. From your text, it sounds like you're trying to do basic "finger painting", where you basically just draw curves that match the input. Your code doesn't look much like that to me, but between the variables you're using that we haven't seen declarations for (stroke and strokes) and the one-letter variable names, I'm a bit lost. Certainly these two lines:

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

使它看起来像你会超过简单的绘图用户用他们的手指画什么。

make it look like you're going for more than simply drawing what the user draws with their finger.

不过,我想尽力帮助。 画圆()真的应该总是画圈圈。点之间画线是要给你尖角(多边形,如果你关闭一个形状)。如果你想顺利得出这样的路径,然后贝塞尔曲线确实是你想要的。

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.

如果您从SDK 下载样品code,有所谓,做了很好的工作,演示如何顺利绘制任意曲线。FingerPaint的一个例子。

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.

这篇关于在画布上的位图的Andr​​oid芯片快速绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆