六边形图片视图 圆角化

3

enter image description here

你好,我想要如上图所示的圆角。我已经成功制作了六边形ImageView,但是我无法将其圆角化。请帮忙看一下代码。我曾尝试使用椭圆ImageView中使用的弧度,但它并没有起作用。我是Android的新手,非常感谢任何帮助。

public class HexagonImageView extends ImageView {

private Path hexagonPath;
private Path hexagonBorderPath;
private float radius;
private Bitmap image;
private int viewWidth;
private int viewHeight;
private Paint paint;
private BitmapShader shader;
private Paint paintBorder;
private int borderWidth = 5;

public HexagonImageView(Context context) {
    super(context);
    setup();
}

public HexagonImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setup();
}

public HexagonImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setup();
}

private void setup() {
    paint = new Paint();
    paint.setAntiAlias(true);

    paintBorder = new Paint();
    setBorderColor(Color.WHITE);
    paintBorder.setAntiAlias(true);
    this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
    paintBorder.setShadowLayer(4.0f, 1.0f, 1.0f, Color.BLACK);

    hexagonPath = new Path();
    hexagonBorderPath = new Path();

}

public void setRadius(float r) {
    this.radius = r;
    calculatePath();
    this.invalidate();
}

public void setBorderWidth(int borderWidth)  {
    this.borderWidth = borderWidth;
    this.invalidate();
}

public void setBorderColor(int borderColor)  {
    if (paintBorder != null)
        paintBorder.setColor(borderColor);

    this.invalidate();
}

private void calculatePath() {

    float triangleHeight = (float) (Math.sqrt(3) * radius / 2);
    float centerX = viewWidth/2;
    float centerY = viewHeight/2;

    hexagonBorderPath.moveTo(centerX, centerY + radius);
    hexagonBorderPath.lineTo(centerX - triangleHeight, centerY + radius/2);
    hexagonBorderPath.lineTo(centerX - triangleHeight, centerY - radius/2);
    hexagonBorderPath.lineTo(centerX, centerY - radius);
    hexagonBorderPath.lineTo(centerX + triangleHeight, centerY - radius/2);
    hexagonBorderPath.lineTo(centerX + triangleHeight, centerY + radius/2);
    hexagonBorderPath.moveTo(centerX, centerY + radius);

    float radiusBorder = radius - borderWidth;    
    float triangleBorderHeight = (float) (Math.sqrt(3) * radiusBorder / 2);

    hexagonPath.moveTo(centerX, centerY + radiusBorder);
    hexagonPath.lineTo(centerX - triangleBorderHeight, centerY + radiusBorder/2);
    hexagonPath.lineTo(centerX - triangleBorderHeight, centerY - radiusBorder/2);
    hexagonPath.lineTo(centerX, centerY - radiusBorder);
    hexagonPath.lineTo(centerX + triangleBorderHeight, centerY - radiusBorder/2);
    hexagonPath.lineTo(centerX + triangleBorderHeight, centerY + radiusBorder/2);
    hexagonPath.moveTo(centerX, centerY + radiusBorder);

    this.invalidate();
}

private void loadBitmap()  {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();

    if (bitmapDrawable != null)
        image = bitmapDrawable.getBitmap();
}

@SuppressLint("DrawAllocation")
@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);

    loadBitmap();

    // init shader
    if (image != null) {

        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), true), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader);

        canvas.drawPath(hexagonBorderPath, paintBorder);
        canvas.drawPath(hexagonPath, paint);
        canvas.clipPath(hexagonPath, Region.Op.DIFFERENCE);
    }

}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = measureWidth(widthMeasureSpec);
    int height = measureHeight(heightMeasureSpec, widthMeasureSpec);

    viewWidth = width - (borderWidth * 2);
    viewHeight = height - (borderWidth * 2);

    radius = height / 2 - borderWidth;

    calculatePath();

    setMeasuredDimension(width, height);
}

private int measureWidth(int measureSpec)   {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY)  {
        result = specSize;
    }
    else {
        result = viewWidth;
    }

    return result;
}

private int measureHeight(int measureSpecHeight, int measureSpecWidth)  {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpecHeight);
    int specSize = MeasureSpec.getSize(measureSpecHeight);

    if (specMode == MeasureSpec.EXACTLY) {
        result = specSize;
    }
    else {
        result = viewHeight;
    }

    return result;
}
}
2个回答

1
你需要用hexagonPath.cubicTohexagonPath.quadTo替换你的hexagonPath.lineTo命令。个人而言,我觉得后者更容易使用。与你的线条绘制一样,都从最后一个点绘制,但是它们分别让你指定2个或1个控制点加上目标点的坐标。控制点会在数据点之间产生弯曲效果。

如果听起来太麻烦(实际比听起来容易),则有一个库可以满足你的需求,参考this answer


嗨 @jason 我还是卡在这个问题上。我尝试了很多方法,但似乎都没有起作用。你能帮我一下吗?能否请你做一个圆角,这样我就可以继续进行了。谢谢 - Rahul Sood
@RahulSood... 你得到了你的答案吗? - Android Develeoper

1

我使用了this

然后我按照你的方式进行了修改。只需调整角度和圆角半径即可获得所需效果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接