带透明边框的菱形按钮

Diamond Shaped Button with transparent borders

我使用 customView class 创建了一个菱形按钮。 在这个 class:

的 onDraw 方法中
 @Override
protected void onDraw(Canvas canvas) {

    mPath.moveTo(mWidth/2 , 0);
    mPath.lineTo(mWidth , mHigh/2);
    mPath.lineTo(mWidth /2 , mHigh);
    mPath.lineTo(0 , mHigh/2);
    mPath.lineTo( mWidth/2 ,0);

    canvas.drawPath(mPath ,mBorderPaint);
    super.onDraw(canvas);

}

borderPaint 定义如下:

    mBorderPaint = new Paint();
    mBorderPaint.setColor(mBorderColor);
    mBorderPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    borderPaint.setStrokeWidth(mBorderWidth);

但我希望我的菱形按钮具有透明边框。我该怎么办?

路径要画两次,先画填充,再画描边。

//initialize the paint object before onDraw method is called
mBorderPaint = new Paint();

@Override
protected void onDraw(Canvas canvas) {

    mPath.moveTo(mWidth/2 , 0);
    mPath.lineTo(mWidth , mHeight/2);
    mPath.lineTo(mWidth /2 , mHeight);
    mPath.lineTo(0 , mHeight/2);
    mPath.lineTo( mWidth/2 ,0);

    //setup the paint for fill
    mBorderPaint.setAlpha(255);
    mBorderPaint.setColor(mBorderColor);
    mBorderPaint.setStyle(Paint.Style.FILL);
    borderPaint.setStrokeWidth(mBorderWidth);

    //draw it
    canvas.drawPath(mPath ,mBorderPaint);

    //setup the paint for stroke
    mBorderPaint.setAlpha(51);
    mBorderPaint.setStyle(Paint.Style.STROKE);

    //draw it again
    canvas.drawPath(mPath ,mBorderPaint);

    super.onDraw(canvas);
}