如何在android中创建自定义形状:交叉圆圈

时间:2014-04-07 07:16:57

标签: java android drawable draw shape

我想绘制一个圆形并通过在Android中使用Shape drawable来交叉,就像image

一样

如果有人知道如何实现。请帮忙。我无法在Circle内创建Cross。

1 个答案:

答案 0 :(得分:2)

  

使用这一个代码

    class MyView extends View {
              Paint paint1 = new Paint();
            Paint paint2 = new Paint();
            Paint paint3 = new Paint();
            Paint paint4 = new Paint();
            Paint paint5 = new Paint();
            final RectF rect = new RectF();
             int mRadius = 130;
    @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
                // Example values
                rect.set(getWidth() / 2 - mRadius, getHeight() / 2 - mRadius,
                        getWidth() / 2 + mRadius, getHeight() / 2 + mRadius);
                paint1.setColor(Color.RED);
                paint1.setStrokeWidth(16);
                paint1.setAntiAlias(true);
                paint1.setStrokeCap(Paint.Cap.BUTT);
                paint1.setStyle(Paint.Style.STROKE);
                paint2.setColor(Color.RED);
                paint2.setStrokeWidth(mRadius / 2);
                paint2.setAntiAlias(true);
                paint2.setStrokeCap(Paint.Cap.BUTT);
                paint2.setStyle(Paint.Style.STROKE);
                paint3.setColor(Color.RED);
                paint3.setStrokeWidth(16);
                paint3.setAntiAlias(true);
                paint3.setStrokeCap(Paint.Cap.BUTT);
                paint3.setStyle(Paint.Style.STROKE);
                canvas.drawArc(rect, 0, 3600, false, paint1);

                canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2
                        - mRadius, getHeight() / 2 - mRadius, paint3);

                canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2
                        + mRadius, getHeight() / 2 - mRadius, paint3);

                canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2
                        - mRadius, getHeight() / 2 + mRadius, paint3);

                canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2
                        + mRadius , getHeight() / 2 + mRadius , paint3);
  }
}

Screenshot