Rainbow Hands

Hand color depends from the its angle

Description

The color of each hand depends from the angle. Simplest code fills the hand rectangle by the new color using OnBeforeDrawListener of TrueAnalogClockHand class.

Anatomy

Simple round face and simplest rectangular hands.

XML Settings File

<?xml version="1.0" encoding="utf-8"?>
<TrueAnalogClock
    face="rainbow_hands_face"
    useSystemTime="true">

    <hand
        image="rainbow_hands_hand_hour"
        axisFacePoint="979:979"
        axisPoint="29:588"
        type="Hour"/>

    <hand
        image="rainbow_hands_hand_minute"
        axisFacePoint="979:979"
        axisPoint="18:955"
        type="Minute"/>

    <hand
        image="rainbow_hands_hand_second"
        axisFacePoint="979:979"
        axisPoint="5:955"
        type="Second"/>

</TrueAnalogClock>

Manual code

The code sets listener and creates new bitmaps for hands.

for (int i = 0; i < clock.getHandCount(); i++) {
  TrueAnalogClockHand.BeforeDrawListener listener = 
    new TrueAnalogClockHand.BeforeDrawListener() {

    private void modifyBitmap(TrueAnalogClockHand hand, int color) {
      Bitmap bitmap = hand.getBitmap();
      bitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
        Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      Paint paint = new Paint();
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(color);
      canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint);
      hand.setBitmap(bitmap);
    }

    @Override
    public Boolean onBeforeDraw(TrueAnalogClockHand hand, Canvas canvas) {
      float[] hsv = new float[3];
      hsv[0] = hand.calculateAngle();
      hsv[1] = 1;
      hsv[2] = 1;
      modifyBitmap(hand, Color.HSVToColor(hsv));
      return false;
    }
  };

  clock.getHand(i).setBeforeDrawListener(listener);
}