关于不同API的Android绘图问题

时间:2013-08-02 20:29:54

标签: android android-canvas paint

我在使用Paint for Android的不同API时遇到了问题。

用户应该能够在一个区域上绘制字母,这在API 8和10上工作正常,但对于API 16和17,这些线条看起来非常不同。我将展示使用图像。

这就是它应该看起来的样子,API 8

这就是它在API 16上的样子。

这是我的绘图视图代码:

public class TouchDrawView extends View
{
    private Paint mPaint;
    private ArrayList<Point> mPoints;
    private ArrayList<ArrayList<Point>> mStrokes;

    public TouchDrawView(Context context)
    {
        super(context);

        mPoints = new ArrayList<Point>();
        mStrokes = new ArrayList<ArrayList<Point>>();
        mPaint = createPaint(Color.BLACK, 14);
    }

    @Override
    public void onDraw(Canvas c)
    {
        super.onDraw(c);

        for(ArrayList<Point> points: mStrokes)
        {
            drawStroke(points, c);
        }

        drawStroke(mPoints, c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if(event.getActionMasked() == MotionEvent.ACTION_MOVE)
        {
            mPoints.add(new Point((int) event.getX(), (int) event.getY()));

            this.invalidate();
        }

        if(event.getActionMasked() == MotionEvent.ACTION_UP)
        {
            mStrokes.add(mPoints);
            mPoints = new ArrayList();
        }

        return true;
    }

    private void drawStroke(ArrayList stroke, Canvas c)
    {
        if (stroke.size() > 0)
        {
            Point p0 = (Point)stroke.get(0);

            for (int i = 1; i < stroke.size(); i++)
            {
                Point p1 = (Point)stroke.get(i);
                c.drawLine(p0.x, p0.y, p1.x, p1.y, mPaint);
                p0 = p1;
            }
        }
    }

    public void clear()
    {
        mPoints.clear();
        mStrokes.clear();

        this.invalidate();
    }

    private Paint createPaint(int color, float width)
    {
        Paint temp = new Paint();
        temp.setStyle(Paint.Style.FILL_AND_STROKE);
        temp.setAntiAlias(true);
        temp.setColor(color);
        temp.setStrokeWidth(width);
        temp.setStrokeCap(Paint.Cap.ROUND);

        return temp;
    }
}

1 个答案:

答案 0 :(得分:3)

好吧,您的应用似乎是硬件加速的,在此模式下,不支持某些功能,如setStrokeCap()(对于行),请查看:http://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported

只需禁用硬件加速,然后重试。这是你禁用它的方法:

<application android:hardwareAccelerated="false" ...>