zaterdag 16 april 2011

Android threading example

Voor mijn tweede android app, was ik op zoek naar wat meer uitdaging.
Een app die gebruik maakt van native cpp calls, en daarmee image genereert.
public class JNIEphi {
 private static native void test4(Bitmap  bitmap, int   arg1, int arg2, int arg3);
 private static native void test6(Bitmap  bitmap, int spools, int colorlevel, int rendermethod);
 private Map mMap = new HashMap();
 private Bitmap mBitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.RGB_565);
    
 public void test4(){
  test4(mBitmap, 2, 3, 4);
 }
 public void test6(int spools, int cl, int rm){
  test6(mBitmap, spools, cl, rm);
 }

 public static JNIEphi getInstance(){
  if (mJNIEphi==null){
    mJNIEphi= new JNIEphi();
  }
  return mJNIEphi;
 }
 private static JNIEphi mJNIEphi;
 
 /* load our native library */
    static {
        System.loadLibrary("sysephi");
    }
}
>

De aanroep in Cpp is als volgt:
static void android_os_Ephi_test6(JNIEnv * env, jobject  obj, jobject bitmap,  jint aElements , jint colorlevel, jint rendermethod)
{
 AndroidBitmapInfo  info;
    void*              pixels;
    int                ret;
    static int         mod_x=0;
    static int         mod_y=0; 
 //
 //
 //
 Statics statics;
 ElectroDynamics dyn(statics);
 
 make_solenoid (statics, aElements, RADIUS, WIRE_RADIUS, DIST6, CURRENT);

 Screen screen(info.width, info.height, 0.9, 0.9);
 Scene scene(dyn);
 scene.calc (screen);
 scene.set_coloring (colorlevel, true);
 if (rendermethod == 0){
  scene.render_map (screen);
 }
 if (rendermethod == 1){
  scene.render_lic (screen);
 }
 //
 // coping result tp android bitmap
 //
 int yy;
 int xx;
 color3 c;
 for (yy = 0; yy < info.height; yy++) {
  uint16_t*  line = (uint16_t*)pixels;
        int xx;
        for (xx = 0; xx < info.width; xx++) {
   c = screen.smap[yy * info.width + xx];
   line[xx] = make565((int)c.r, (int)c.g, (int)c.b);
        }
  pixels = (char*)pixels + info.stride;
 }
 AndroidBitmap_unlockPixels(env, bitmap);  
}
>




Het is gebaseerd op bestaande software Ephi, wat geheel in cpp is geschreven.
Ephi is gecompileerd als lib en wordt vanuit de android app aangeroepen.


In de cpp aanroep wordt een berekening uitgevoerd, waarna het resultaat op een bitmap getekend wordt, het berekenen neemt enige tijd in beslag en wordt in een aparte thread uitgevoerd.

Tijdens het berekenen verschijnt een dialoog.
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //..
  progressDialog = new ProgressDialog(this);
  progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  progressDialog.setTitle("SysEphi Running...");
  progressDialog.setCancelable(false);
 }
>

Voor het starten en aan het einde van de task wordt de progressbar aangeroepen.
 public void showProgressbar(final boolean show, final String text) {
  guiThread.post(new Runnable() {
   public void run() {
    if (show) {
     progressDialog.setMessage(text);
     progressDialog.show();
    } else {
     progressDialog.hide();
     TabActivity ta= (TabActivity)SettingsActivity.this.getParent();
     TabHost tabHost = ta.getTabHost();
     tabHost.setCurrentTab(0);
    }
   }
  });
 }
 
>



Het eind resultaat:

De apk file downloaden kan hieronder, deze werkt op 2.2 of hoger, 2.1 gaat niet werken deze mist libjnigrapics.so.
SysEphi Beta Release

Geen opmerkingen:

Een reactie posten