Home | History | Annotate | Download | only in example
      1 package com.example;
      2 
      3 import java.util.Timer;
      4 import java.util.TimerTask;
      5 
      6 import android.app.Activity;
      7 import android.content.Context;
      8 import android.graphics.Bitmap;
      9 import android.graphics.Canvas;
     10 import android.os.Bundle;
     11 import android.os.SystemClock;
     12 import android.util.Log;
     13 import android.view.View;
     14 
     15 public class HelloSkiaActivity extends Activity
     16 {
     17     private SkiaDrawView fMainView;
     18 
     19     /** Called when the activity is first created. */
     20     @Override
     21     public void onCreate(Bundle savedInstanceState) {
     22         super.onCreate(savedInstanceState);
     23 
     24         // Makes and sets a SkiaDrawView as the only thing seen in this activity
     25         fMainView = new SkiaDrawView(this);
     26         setContentView(fMainView);
     27 
     28         try {
     29             // Load Skia and then the app shared object in this order
     30             System.loadLibrary("skia_android");
     31             System.loadLibrary("hello_skia_ndk");
     32 
     33         } catch (UnsatisfiedLinkError e) {
     34             Log.d("HelloSkia", "Link Error: " + e);
     35             return;
     36         }
     37 
     38         // Set a timer that will periodically request an update of the SkiaDrawView
     39         Timer fAnimationTimer = new Timer();
     40         fAnimationTimer.schedule(new TimerTask() {
     41             public void run()
     42             {
     43                 // This will request an update of the SkiaDrawView, even from other threads
     44                 fMainView.postInvalidate();
     45             }
     46         }, 0, 5); // 0 means no delay before the timer starts; 5 means repeat every 5 milliseconds
     47     }
     48 
     49     private class SkiaDrawView extends View {
     50         Bitmap fSkiaBitmap;
     51         public SkiaDrawView(Context ctx) {
     52             super(ctx);
     53         }
     54 
     55         @Override
     56         protected void onSizeChanged(int w, int h, int oldw, int oldh)
     57         {
     58             // Create a bitmap for skia to draw into
     59             fSkiaBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
     60         }
     61 
     62         @Override
     63         protected void onDraw(Canvas canvas) {
     64             // Call into our C++ code that renders to the bitmap using Skia
     65             drawIntoBitmap(fSkiaBitmap, SystemClock.elapsedRealtime());
     66 
     67             // Present the bitmap on the screen
     68             canvas.drawBitmap(fSkiaBitmap, 0, 0, null);
     69         }
     70     }
     71 
     72 
     73     private native void drawIntoBitmap(Bitmap image, long elapsedTime);
     74 }
     75