public class

ImageScript

extends Image
java.lang.Object
   ↳ net.pierrox.lightning_launcher.script.api.Image
     ↳ net.pierrox.lightning_launcher.script.api.ImageScript

Class Overview

ImageScript is a way to draw images or animations without the need for intermediate bitmaps. Such images are scalable and memory efficient. Instances of ImageScript can be created with createImage(Scriptable, int, int). The Scriptable object must have a "draw" function, and optionally a "pause" and "resume" functions. These functions are called with a DrawingContext instance.

Note: pause and resume functions are called when the drawing is assigned to item icons only, not for backgrounds not icon layers. Animated backgrounds are currently not supported.

Sample 1 : draw a static image.

 var img = LL.createImage({
     draw: function(context) {
         var canvas = context.getCanvas();
         var w = context.getWidth();
         var h = context.getHeight();
         var cx = w/2;
         var cy = h/2;

         var p = new Paint();
         p.setAntiAlias(true);

         var step = 1;
         canvas.save();
         for(var angle = 0; angle < 180; angle += step) {
             p.setColor(Color.HSVToColor([angle*2, 1, 1]));
             canvas.rotate(step, cx, cy);
             canvas.drawLine(0, cy, w, cy, p);
         }
         canvas.restore();
     }
 }, -1, -1);
 


Sample 2 : this is a more complex script. It keeps tracks of timers per drawing context in order to create an animation that can be shared between several items
 var drawing = {
      draw: function(context) {
          var canvas = context.getCanvas();
          var w = context.getWidth();
          var h = context.getHeight();
          var cx = w/2;
          var cy = h/2;

          var p = new Paint();
          p.setAntiAlias(true);
          p.setStrokeWidth(4);

          var step = 4;
          canvas.save();
          for(var angle = 0; angle < 180; angle += step) {
              var hue = ((angle+drawing.shift)*2)%360
              p.setColor(Color.HSVToColor([hue, 1, 1]));
              canvas.rotate(step, cx, cy);
              canvas.drawLine(0, cy, w, cy, p);
          }
          canvas.restore();
      },

      resume: function(context) {
          var animate = function() {
              context.invalidate();
              var id = ""+context.getId();
              drawing.shift++;
              drawing.timers[id] = setTimeout(animate, 100);
          };
          animate();
      },

      pause: function(context) {
          var id = ""+context.getId();
          clearTimeout(drawing.timers[id]);
          delete drawing.timers[id];
      },

      shift: 0,
      timers: {}
 };

 var img = LL.createImage(drawing, -1, -1);
 

Summary

Nested Classes
class ImageScript.DrawingContext The DrawingContext is the link between the drawing script and the drawing target. 
Public Methods
int getHeight()
Return the image height
Scriptable getObject()
Return the object containing functions used to handle the drawing operations.
int getWidth()
Return the image width
void invalidate()
Request this image to be drawn again, at some point in the future.
[Expand]
Inherited Methods
From class net.pierrox.lightning_launcher.script.api.Image
From class java.lang.Object

Public Methods

public int getHeight ()

Return the image height

public Scriptable getObject ()

Return the object containing functions used to handle the drawing operations.

public int getWidth ()

Return the image width

public void invalidate ()

Request this image to be drawn again, at some point in the future.