Android 如何从给定半径绘制圆

Android 如何从给定半径绘制圆,android,android-canvas,audio-recording,Android,Android Canvas,Audio Recording,我有一个振幅值,是从其他类的音频信号中得到的。它可以通过调用circlerender中的函数getAmp获得该值。现在,我有一个画圆圈的班。我想画一个圆,它的半径由振幅值设定。你能看看我的课,并建议我如何修改它吗。目前,我只是比较阈值来决定是否绘制 public class CircleRenderer extends Renderer { private Paint mPaint; private boolean mCycleColor; priva

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

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

    }
公共类CirclerEnder扩展渲染器
{
私人油漆;
私有布尔mCycleColor;
私有int AmplicateThreshold=5;
私人内部amp=0;
/**
*将音频数据渲染到脉冲圆上
*@param画布
*@param paint-使用绘制线进行绘制
*/
公共电路制造商(油漆)
{
这个(油漆,假的);
}
公共无效getAmp(int dbAmp)
{
amp=dbAmp;
}
/**
*将音频数据渲染到脉冲圆上
*@param画布
*@param paint-使用绘制线进行绘制
*@param cycleColor-如果为真,则每帧的颜色都会改变
*/
公共电路布线器(油漆,布尔色)
{
超级();
mPaint=油漆;
McCycleColor=cycleColor;
}
@凌驾
公共void onRender(画布、音频数据、Rect-Rect)
{
if(mCycleColor)
{
cycleColor();
}
对于(int i=0;iAmpliteThreshold)
{
//振幅比正常值大,画一条突出的线
画布.抽绳(MPoint,MPoint);
}
//控制脉冲频率
调制+=0.04;
}
浮点调制=0;
浮子侵蚀性=0.33f;
私有浮动[]拓扑(浮动[]笛卡尔,矩形)
{
双cX=矩形宽度()/2;
double cY=垂直高度()/2;
双角度=(笛卡尔[0])*2*Math.PI;
双半径=((矩形宽度()/2)*(1-侵略)+侵略*笛卡尔[1]/2)*(1.2+数学正弦(调制))/2.2;
浮动[]出={
(浮动)(cX+半径*数学正弦(角度)),
(浮动)(cY+半径*数学坐标(角度))
};
返回;
}
专用浮点彩色计数器=0;
私有void cycleColor()
{
intr=(int)数学地板(128*(数学sin(颜色计数器)+1));
intg=(int)数学地板(128*(数学sin(颜色计数器+2)+1));
intb=(int)数学地板(128*(数学sin(颜色计数器+4)+1));
mPaint.setColor(Color.argb(128,r,g,b));
彩色计数器+=0.03;
}  
}

使用canvas.drawCircle进行此操作

protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);

   canvas.drawCircle(x, y, radius, paint);
}
编辑: 有关更多信息,请查阅


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

使用如何?实际上,我想通过使用画布来进行一些可视化。@IntelliJAmiya:我使用画布来完成该任务。这里的问题是,我无法控制圆的大小以及它何时绘制或不绘制。我需要使用上面的代码来创建一些动画为什么你要发布一个链接。。。其中显示的代码与您已经发布的代码相同?实际上,我想进行一些可视化,从第号开始。我修改了指向我任务的链接。该链接中的代码每次仅绘制圆。在我的任务中,它将根据半径绘制,半径是信号的安培数。我认为像我上面发布的那样做更容易。要恢复画布(对于新的amp值),可以使用“canvas.restore();”(我认为之前保存它是不必要的,因为您希望用较小/较大的半径重新绘制圆)。我找到了一个解决方案,即double radius=amp;但它将失去一些动画,这是我任务的主要目标