我在使用适用于 Android 的不同 API 的 Paint 时遇到问题。
用户应该能够在一个区域上绘制字母,这在 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;
}
}