title: Drawing a custom circle segment dynamically
body: <p>My customer wants the following widget in the application:<br />
  <img src="https://i.stack.imgur.com/SQnWp.png" alt="enter image description here" />
  <br />The <code>Text</code> comes from the server. The gradient angle depends on variables which also comes from the server. Also, customer wants the gradient to be filled dynamically (a user must see how the gradient is filled starting from the 0).
  <br />
  Now I do the following: I use two images - one is the colored circle, the second is the grey circle. I create a circle segment with a certain angle and apply it as a mask to the grey circle, then combine the colored circle with the new grey circle (where a sector is cut off).<br />
  Here is my code. I initialize the variables calling the <code>initializeVarsForCompoundImDrawing</code>, then a few times in a second call the <code>makeCompoundImage</code> and in the end call the <code>nullVarsForCompoundImDrawing</code> to free the resources: <br /><br /></p>
  <pre><code>private static Bitmap notColoredBitmap;
  private static Bitmap coloredBitmap;
  private static Bitmap notColoredWithMaskBitmap;
  private static Bitmap finalBitmap;
  private static Canvas notColoredWithMaskCanvas;
  private static Paint paintForMask;
  private static Paint smoothPaint;
  private static Canvas finalCanvas;
  private static RectF rectForMask;
  
  public static void initializeVarsForCompoundImDrawing()
  {
      Context context = MainApplication.getContext();
      notColoredBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.not_colored);
      coloredBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.colored);
      
      paintForMask = new Paint(Paint.ANTI_ALIAS_FLAG);
      paintForMask.setStyle(Paint.Style.FILL);
      paintForMask.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
      rectForMask = new RectF(0, 0, notColoredBitmap.getWidth(), notColoredBitmap.getHeight());
  
      smoothPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  }
  
  public static void nullVarsForCompoundImDrawing()
  {
      notColoredBitmap = null;
      coloredBitmap = null;
      
      paintForMask = null;
      rectForMask = null;
      smoothPaint = null;
  }
  
  public static void makeCompoundImage(ImageView imageView, int angle)
  {
      notColoredWithMaskBitmap = Bitmap.createBitmap(notColoredBitmap.getWidth(), notColoredBitmap.getHeight(), Bitmap.Config.ARGB_8888);
      notColoredWithMaskCanvas = new Canvas(notColoredWithMaskBitmap);
      notColoredWithMaskCanvas.drawBitmap(notColoredBitmap, 0, 0, smoothPaint);
      notColoredWithMaskCanvas.drawArc(rectForMask, 270, angle, true, paintForMask);
  
      finalBitmap = Bitmap.createBitmap(notColoredBitmap.getWidth(), notColoredBitmap.getHeight(), Bitmap.Config.ARGB_8888);
      finalCanvas = new Canvas(finalBitmap);
      finalCanvas.drawBitmap(coloredBitmap, 0, 0, smoothPaint);
      finalCanvas.drawBitmap(notColoredWithMaskBitmap, 0, 0, smoothPaint);
  
      imageView.setImageBitmap(finalBitmap);
  }
  </code></pre>
  <p>The first question: is it possible to improve this code to use less resources?<br />
  The second question: how can I add the text to the <code>finalBitmap</code> (now it is a <code>TextView</code> which is shown at the top of the <code>ImageView</code> with the image)?</p>