如何从给定半径绘制圆

时间:2015-09-03 07:54:07

标签: android android-canvas audio-recording

我的振幅值来自其他类别的音频信号。它可以通过调用getAmp中的函数CircleRenderer来获取该值。现在,我有一个CircleRenderer类来绘制圆圈。我想绘制一个圆圈,其半径由振幅值设定。你能看到我的课,并建议我如何修改它。目前,我只是与阈值进行比较来决定是否绘制。

public class CircleRenderer extends Renderer
    {
      private Paint mPaint;
      private boolean mCycleColor;
      private int amplitudeThreshold = 5;
      private int amp = 0;
      /**
       * Renders the audio data onto a pulsing circle
       * @param canvas
       * @param paint - Paint to draw lines with
       */
      public CircleRenderer(Paint paint)
      {
        this(paint, false);
      }
      public void getAmp(int dbAmp)
      {
        amp=dbAmp;
      }
      /**
       * Renders the audio data onto a pulsing circle
       * @param canvas
       * @param paint - Paint to draw lines with
       * @param cycleColor - If true the color will change on each frame
       */
      public CircleRenderer(Paint paint, boolean cycleColor)
      {
        super();
        mPaint = paint;
        mCycleColor = cycleColor;
      }

      @Override
      public void onRender(Canvas canvas, AudioData data, Rect rect)
      {
        if(mCycleColor)
        {
          cycleColor();
        }

        for (int i = 0; i < data.bytes.length - 1; i++) {
          float[] cartPoint = {
              (float)i / (data.bytes.length - 1),
              rect.height() / 2 + ((byte) (data.bytes[i] + 128)) * (rect.height() / 2) / 128
          };

          float[] polarPoint = toPolar(cartPoint, rect);
          mPoints[i * 4] = polarPoint[0];
          mPoints[i * 4 + 1] = polarPoint[1];

          float[] cartPoint2 = {
              (float)(i + 1) / (data.bytes.length - 1),
              rect.height() / 2 + ((byte) (data.bytes[i + 1] + 128)) * (rect.height() / 2) / 128
          };

          float[] polarPoint2 = toPolar(cartPoint2, rect);
          mPoints[i * 4 + 2] = polarPoint2[0];
          mPoints[i * 4 + 3] = polarPoint2[1];
        }

        if(amp > amplitudeThreshold )
        {
          // Amplitude is bigger than normal, make a prominent line
          canvas.drawLines(mPoints, mPaint);
        }


        // Controls the pulsing rate
        modulation += 0.04;
      }


      float modulation = 0;
      float aggresive = 0.33f;
      private float[] toPolar(float[] cartesian, Rect rect)
      {
        double cX = rect.width()/2;
        double cY = rect.height()/2;
        double angle = (cartesian[0]) * 2 * Math.PI;
        double radius = ((rect.width()/2) * (1 - aggresive) + aggresive * cartesian[1]/2) * (1.2 + Math.sin(modulation))/2.2;
        float[] out =  {
            (float)(cX + radius * Math.sin(angle)),
            (float)(cY + radius * Math.cos(angle))
        };
        return out;
      }

      private float colorCounter = 0;
      private void cycleColor()
      {
        int r = (int)Math.floor(128*(Math.sin(colorCounter) + 1));
        int g = (int)Math.floor(128*(Math.sin(colorCounter + 2) + 1));
        int b = (int)Math.floor(128*(Math.sin(colorCounter + 4) + 1));
        mPaint.setColor(Color.argb(128, r, g, b));
        colorCounter += 0.03;
      }  

    }

1 个答案:

答案 0 :(得分:0)

使用canvas.drawCircle

执行此操作
protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);

   canvas.drawCircle(x, y, radius, paint);
}

编辑: 有关详细信息,您可以查找official docs

Here是另一个用画布绘制圆圈的教程。

相关问题