Example #1
0
  @Override
  public void onDraw(Canvas canvas) {

    // If down, draw opaque. Otherwise draw transparent.
    canvas.drawBitmap(buttonBitmap, 0, 0, isDown ? parent.downPaint : parent.upPaint);
    super.onDraw(canvas);
  }
Example #2
0
  /**
   * Generates bitmap used for drawing button.
   *
   * @param radius Radius of button in pixels.
   */
  @Override
  void generateBitmap() {
    super.generateBitmap();
    // Size and shape
    buttonShape.setShape(new OvalShape());
    buttonShape.setBounds(
        new Rect(PADDING, PADDING, (int) radius * 2 - PADDING, (int) radius * 2 - PADDING));

    // Create button bitmap and render shape
    buttonBitmap = Bitmap.createBitmap((int) radius * 2, (int) radius * 2, Bitmap.Config.ARGB_4444);
    Canvas handleCanvas = new Canvas(buttonBitmap);
    buttonShape.getPaint().set(parent.fillPaint);
    buttonShape.draw(handleCanvas);

    // Set text style
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(Typeface.DEFAULT_BOLD);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setColor(Color.BLACK);
    // Set size (one character has a standard design, more has dynamic size)
    if (label.length() == 1) textPaint.setTextSize(radius);
    else textPaint.setTextSize(radius / label.length() * 2.5f);

    // Set paint to clear text from button
    textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    // Render the button
    handleCanvas.drawText(label, radius, radius + (textPaint.getTextSize() * .33333f), textPaint);
  }
Example #3
0
 @Override
 void writeXml(XmlSerializer serializer, OutputStream fileos)
     throws IllegalArgumentException, IllegalStateException, IOException {
   serializer.startTag(null, "button");
   super.writeXml(serializer, fileos);
   // set an attribute called "attribute" with a "value" for <child2>
   serializer.attribute(null, "label", label);
   serializer.attribute(null, "buttonIndex", Integer.toString(buttonIndex));
   serializer.endTag(null, "button");
 }