android - 带透明边框的菱形按钮

标签 android view android-custom-view

我使用了 customView 类来创建菱形按钮。 在该类的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);
}

关于android - 带透明边框的菱形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28898906/

相关文章:

android - 使用 librtmp 为 android 构建 FFMPEG

android - 如何使用 isInEditMode() 在编辑器中查看带有自定义 View 的布局

ios - 从编程按钮问题中呈现 UIViewController

android - 如何在工具栏下方添加 View ?

android - 设置Android Studio预览背景颜色

Android 拍照并将其存储为字节数组

javascript - 溢出-x :hidden does not work on mobile devices

Asp.net 在移动平台上下载文件

java - 如何在 Hibernate 中使用临时表或 View ?

mysql - 如何以相同的形式搜索和查看数据