在Android中使用六角形Imageview的问题?

时间:2015-07-30 12:23:28

标签: android bitmap imageview

我为ImageView赋予六边形形状做了很多事情,但这不起作用。我有一些问题在给定的波纹图像中显示。

enter image description here

我试试这个波纹管代码。告诉我我在哪里做错了?

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();
}

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 - 1);
    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 - 5;
    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);

    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(), false),
                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader);

        canvas.drawPath(hexagonBorderPath, paintBorder);
        canvas.drawPath(hexagonPath, paint);
    }

}

@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);
}

}

和这样的XML文件代码。

<com.android.myproject.HexagonImageView
            android:id="@+id/iv_ProfilePic"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:src="@drawable/default_profile" />

2 个答案:

答案 0 :(得分:2)

我相信我发现了这个问题。您需要在hexagonPath中致电hexagonPath.reset();重置calculatePath(),请参阅下面的代码:

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

    hexagonPath.reset(); // Reset the path

    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);

    invalidate();
    ....

答案 1 :(得分:0)

有两种方法可以做到这一点: -

1-看看这个答案: - https://stackoverflow.com/a/22987264/1384010

2-尝试使用此库以避免所有不必要的麻烦。 https://github.com/siyamed/android-shape-imageview