按钮上的onDraw三角形

时间:2015-07-12 06:39:10

标签: android android-canvas

在我创建的扩展Button的类中,我覆盖了onDraw函数,在按钮右侧创建了一个指向中心的三角形:
Desired result

相反,这就是我得到的:
Actual result

这就是我写的:

Paint paint = new Paint();

@Override
public void onDraw(Canvas canvas)
{
    Paint paint = new Paint();

    paint.setColor(android.graphics.Color.BLACK);
    canvas.drawPaint(paint);

    paint.setStrokeWidth(4);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setAntiAlias(true);

    Point center = new Point(getWidth()/2, getHeight()/2);    
    Point a=new Point(getWidth(), 0);;
    Point b=new Point(getWidth(), getHeight());;

    paint.setColor(Color.RED);    

    Path path= new Path();
    path.setFillType(Path.FillType.EVEN_ODD);
    path.lineTo(b.x, b.y);
    path.lineTo(center.x, center.y);
    path.lineTo(a.x, a.y);
    path.close();
    canvas.drawPath(path, paint);
}

1 个答案:

答案 0 :(得分:2)

你忘记了它的初始点。

这样的东西
path.moveTo(point1_draw.x,point1_draw.y);
在您的第一个lineTo()

之前

因为lineTo()需要"最后一点"开始细分。

void lineTo(float x, float y) // Add a line from the last point to the specified point (x,y).

所以,最后,你的鳕鱼看起来像:

    Path path= new Path();
    path.setFillType(Path.FillType.EVEN_ODD);
    path.moveTo(a.x, a.y);           // Move to a
    path.lineTo(center.x, center.y); // Segment from a to center
    path.lineTo(b.x, b.y);           // Segment from center to b
    path.close();                    // Segment from b to a
    canvas.drawPath(path, paint);