Aegidius
 Plüss   Aplulogo
     
 www.aplu.ch      Print Text 
© 2021, V10.4
 
  JDroidLib
 
 

The source code of all examples is included in the JDroidLib distribution.

Creating User Defined Controls

JDroidLib's Actor class may be used to create animated GUI controls. In the simplest case the control only consists of predefined sprite images (more than one image may be used to animate the control). The library button class GGPushBase and all derived button classes (GGPushButton, GGCheckButton, etc.) use this approach. Some kind of controls must change their appearence heavily during run-time depending on some dynamically changing values (e.g. for a progress bar), so the appearance of the control must be created dynamically. The class GGBitmap provides standard graphics methods to generate the graphics in an internal Bitmap instance. This bitmap is then passed to the constructor of the Actor instance to create the control.

Using this procedure, we construct a simple progress bar that can be integrated in any JDroidLib app.

package ch.aplu.tut;

import ch.aplu.android.*;
import android.graphics.*;

public class ProgressBar
{
  private GameGrid gg;
  private Actor barActor = null;
  private TextActor textActor = null;
  private Location loc;
  private int width;
  private int height;

  public ProgressBar(GameGrid gg, Location loc,
                     
int width, int height)
  {
    this.gg = gg;
    this.loc = loc;
    this.width = width;
    this.height = height;
  }

  public void setValue(int value)
  {
    value = Math.max(0, Math.min(100, value)); 
    // restricted to 0..100
    if (barActor != null)
    {
      barActor.removeSelf();
      textActor.removeSelf();
    }
    barActor = new Actor(createActor(width, height, value));
    gg.addActorNoRefresh(barActor, loc);
    textActor = new TextActor(value + "%");
    int w = textActor.getTextWidth() / 2;
    textActor.setLocationOffset(new Point(-w, 0));
    gg.addActor(textActor, loc);
  }

  private Bitmap createActor(int width, int height, int value)
  {
    int length = value * (width - 1) / 100;
    GGBitmap bm = new GGBitmap(width + 1, height + 1);
    bm.setPaintColor(Color.WHITE);
    bm.drawRectangle(
        new Point(0, 0), new Point(width, height));
    bm.setPaintColor(Color.BLUE);
    bm.fillRectangle(new Point(1, 1), 
        
new Point(length, height - 1));
    return bm.getBitmap();
  }
}

The graphics consists of a white frame and inside a blue rectangle that indicates the current value (0..100 percent). Some special care must be taken to overcome the off-by-one-error for the pixel accurate graphics. We even add a text indicator in the middle of the bar to display the current value. It is up to you to make the appearence more fancy.

For the demonstration we put the progress bar inside a otherwise empty window and advance its value every 50 ms.

package ch.aplu.tut;

import ch.aplu.android.*;

public class Ex05 extends GameGrid
{
  public Ex05()
  {
    super(WHITE);
  }

  public void main()
  {
    getBg().clear(GRAY);
    int w = getNbHorzCells();
    int h = getNbVertCells();
    int value = 0;
    ProgressBar bar = 
      new ProgressBar(thisnew Location(w / 2, h / 2), 200, 25);
    while (value <= 100)
    {
      bar.setValue(value);
      value += 1;
      delay(50);
    }
    showToast("Done");
  }
}

Download Android app for installation on a smartphone or emulator.
Download sources (ProgressBar.zip).
Create QR code to download Android app to your smartphone.
Install/Start
app on a USB connected smartphone or a running emulator.

(This is a WebStart signed by the University of Berne, Switzerland. It installs some helper files in <userhome>.jdroidtools.If you did not install the Android SDK, you may install a slim version of the Android-Emulator in <userhome>.jdroidemul using this link, To start the emulator, execute ExecEmul.jar found in <userhome>.jdroidemul)
Download app via Bluetooth using DroidInstall app.
(This is a WebStart signed by the University of Berne, Switzerland. It starts a Java Bluetooth client application ApkBlueInstaller that connects to a Android Bluetooth server DroidInstall that must be started on the smartphone. Click here to get the DroidInstall app.)

(Sorry, not spectacular, no animation here, better install on your smartphone)

  progressbar