我需要使用此边框
制作此图像视图带有此边框的多边形图像视图:
我使用此自定义六边形蒙版图像视图创建了此类图像视图。
public class HexagonMaskView extends android.support.v7.widget.AppCompatImageView {
private Path hexagonPath;
private Path hexagonBorderPath;
private Paint mBorderPaint,mEdgesPaint;
float radiusX;
public HexagonMaskView(Context context) {
super(context);
init();
}
public HexagonMaskView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HexagonMaskView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
this.hexagonPath = new Path();
this.hexagonBorderPath = new Path();
this.mBorderPaint = new Paint();
this.mBorderPaint.setColor(getResources().getColor(R.color.colorAccent));
this.mBorderPaint.setStrokeCap(Paint.Cap.ROUND);
this.mBorderPaint.setStrokeWidth(40f);
this.mBorderPaint.setStyle(Paint.Style.STROKE);
this.mEdgesPaint = new Paint();
this.mEdgesPaint.setColor(getResources().getColor(R.color.colorAccent));
this.mEdgesPaint.setStrokeCap(Paint.Cap.ROUND);
this.mEdgesPaint.setStrokeWidth(15f);
this.mEdgesPaint.setStyle(Paint.Style.STROKE);
}
private void calculatePath(float radius) {
radiusX = radius;
mBorderPaint.setStrokeWidth(radius/3);
mEdgesPaint.setStrokeWidth(radius/6);
float halfRadius = radius / 2f;
float triangleHeight = (float) (Math.sqrt(3.0) * halfRadius);
float centerX = getMeasuredWidth() / 2f;
float centerY = getMeasuredHeight() / 2f;
this.hexagonPath.reset();
this.hexagonPath.moveTo(centerX, centerY + radius);
this.hexagonPath.lineTo(centerX - triangleHeight, centerY + halfRadius);
this.hexagonPath.lineTo(centerX - triangleHeight, centerY - halfRadius);
this.hexagonPath.lineTo(centerX, centerY - radius);
this.hexagonPath.lineTo(centerX + triangleHeight, centerY - halfRadius);
this.hexagonPath.lineTo(centerX + triangleHeight, centerY + halfRadius);
this.hexagonPath.close();
float radiusBorder = radius - 5f;
float halfRadiusBorder = radiusBorder / 2f;
float triangleBorderHeight = (float) (Math.sqrt(3.0) * halfRadiusBorder);
this.hexagonBorderPath.reset();
this.hexagonBorderPath.moveTo(centerX, centerY + radiusBorder);
this.hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY + halfRadiusBorder);
this.hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY - halfRadiusBorder);
this.hexagonBorderPath.lineTo(centerX, centerY - radiusBorder);
this.hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY - halfRadiusBorder);
this.hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY + halfRadiusBorder);
this.hexagonBorderPath.close();
invalidate();
}
@Override
public void onDraw(Canvas c) {
float halfRadiusBorder = radiusX / 2f;
float radiusBorder = radiusX - 5f;
float triangleBorderHeight = (float) (Math.sqrt(3.0) * halfRadiusBorder);
float centerX = getMeasuredWidth() / 2f;
float centerY = getMeasuredHeight() / 2f;
c.clipPath(hexagonPath, Region.Op.INTERSECT);
c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
super.onDraw(c);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
calculatePath(Math.min(width / 2f, height / 2f) - 10f);
}
此代码生成一个自定义图像视图,其形状与我想要的完全一样,但问题是我无法正确应用边框,因为屏幕尺寸会更改边框,如果应用为图像叠加层则会出现尺寸问题它不直接应用于图像视图
这就是我如何使用相对布局来制作我想要的东西
<RelativeLayout
android:id="@+id/frameCont"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.dev.mytechnology.hellostream.Helper.HexagonMaskView
android:id="@+id/image"
android:layout_width="@dimen/_43sdp"
android:layout_height="@dimen/_43sdp"
android:src="@drawable/sample_face_1_dummy"
android:layout_centerInParent="true"
android:background="@android:color/transparent"/>
<ImageView
android:id="@+id/frame"
android:layout_width="@dimen/_45sdp"
android:layout_height="@dimen/_45sdp"
android:layout_centerInParent="true"
android:src="@drawable/hs_hexa_frame_white" />
</RelativeLayout>
这是由此产生的ui:
我知道我没有使用最优化的方式,但请告诉我如何在我的六角形图像视图上应用这样的边框或任何其他选项也将受到赞赏。