onOffsetsChanged + Parallax Background

Problems with Canvas, OpenGL, etc...

onOffsetsChanged + Parallax Background

Postby toebee » Wed Dec 22, 2010 4:14 am

I want to create a live wallpaper that has three parallaxing backgrounds. They should all be static until a user swipes to a different home screen. At which point the backgrounds scroll with the swipe. I am new to java but know other languages like PHP and JavaScript. I am very interested in learning. So I used googles cube wallpaper example. The problem I'm having is it seems very slow and jerky. Now I am only testing on an emulator at the moment so maybe thats why, but I have a feeling its probably my code. Is anyone willing to look over what I have so far?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.graphics.Bitmap;
  2. import android.graphics.BitmapFactory;
  3. import android.graphics.Canvas;
  4. import android.os.Handler;
  5. import android.service.wallpaper.WallpaperService;
  6. import android.view.MotionEvent;
  7. import android.view.SurfaceHolder;
  8.  
  9. public class LW1 extends WallpaperService {
  10.  
  11.     private final Handler mHandler = new Handler();
  12.  
  13.     @Override
  14.     public void onCreate() {
  15.         super.onCreate();
  16.     }
  17.  
  18.     @Override
  19.     public void onDestroy() {
  20.         super.onDestroy();
  21.     }
  22.  
  23.     @Override
  24.     public Engine onCreateEngine() {
  25.         return new ParallaxEngine();
  26.     }
  27.  
  28.     class ParallaxEngine extends Engine {
  29.  
  30.         private float mOffset;
  31.         private float mCenterX;
  32.         private float mCenterY;
  33.  
  34.         private final Runnable mDrawParallax = new Runnable() {
  35.             public void run() {
  36.                 drawBackgrounds();
  37.             }
  38.         };
  39.         private boolean mVisible;
  40.  
  41.  
  42.  
  43.         @Override
  44.         public void onCreate(SurfaceHolder surfaceHolder) {
  45.             super.onCreate(surfaceHolder);
  46.             // By default we don't get touch events, so enable them.
  47.             setTouchEventsEnabled(true);
  48.         }
  49.  
  50.         @Override
  51.         public void onDestroy() {
  52.             super.onDestroy();
  53.             mHandler.removeCallbacks(mDrawParallax);
  54.         }
  55.  
  56.         @Override
  57.         public void onVisibilityChanged(boolean visible) {
  58.             mVisible = visible;
  59.             if (visible) {
  60.                 drawBackgrounds();
  61.             } else {
  62.                 mHandler.removeCallbacks(mDrawParallax);
  63.             }
  64.         }
  65.  
  66.         @Override
  67.         public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  68.             super.onSurfaceChanged(holder, format, width, height);
  69.             // store the center of the surface. I'm sure there's a better way to center things?
  70.             mCenterX = width/2.0f;
  71.             mCenterY = height/2.0f;
  72.             drawBackgrounds();
  73.         }
  74.  
  75.         @Override
  76.         public void onSurfaceCreated(SurfaceHolder holder) {
  77.             super.onSurfaceCreated(holder);
  78.         }
  79.  
  80.         @Override
  81.         public void onSurfaceDestroyed(SurfaceHolder holder) {
  82.             super.onSurfaceDestroyed(holder);
  83.             mVisible = false;
  84.             mHandler.removeCallbacks(mDrawParallax);
  85.         }
  86.  
  87.         @Override
  88.         public void onOffsetsChanged(float xOffset, float yOffset,
  89.                 float xStep, float yStep, int xPixels, int yPixels) {
  90.             mOffset = xOffset;
  91.             drawBackgrounds();
  92.         }
  93.         /*
  94.          * Store the position of the touch event so we can use it for drawing later
  95.          */
  96.         @Override
  97.         public void onTouchEvent(MotionEvent event) {
  98.             super.onTouchEvent(event);
  99.         }
  100.  
  101.         /*
  102.          * Draw one frame of the animation. This method gets called repeatedly
  103.          * by posting a delayed Runnable. You can do any drawing you want in
  104.          * here.
  105.          */
  106.         void drawBackgrounds() {
  107.             final SurfaceHolder holder = getSurfaceHolder();
  108.  
  109.             Canvas c = null;
  110.             try {
  111.                 c = holder.lockCanvas();
  112.                 if (c != null) {
  113.                     // draw something
  114.                     drawParallax(c);
  115.                 }
  116.             } finally {
  117.                 if (c != null) holder.unlockCanvasAndPost(c);
  118.             }
  119.  
  120.             // Reschedule the next redraw
  121.             mHandler.removeCallbacks(mDrawParallax);
  122.             if (mVisible) {
  123.                 mHandler.postDelayed(mDrawParallax, 1000 / 25);
  124.             }
  125.         }
  126.  
  127.         /*
  128.          * Draw three background images each scrolling at a different speed
  129.          */
  130.         void drawParallax(Canvas c) {
  131.             c.save();
  132.            
  133.             Bitmap _back = BitmapFactory.decodeResource(getResources(), R.drawable.parallax_background_layer_back);
  134.             Bitmap _mid = BitmapFactory.decodeResource(getResources(), R.drawable.parallax_background_layer_mid);
  135.             Bitmap _front = BitmapFactory.decodeResource(getResources(), R.drawable.parallax_background_layer_front);
  136.            
  137.             //Centering the images, again I feel this is probably not the best way
  138.             float backWidth = _back.getWidth()/2;
  139.             float backHeight = _back.getHeight()/2;
  140.             c.translate(mCenterX - backWidth, mCenterY - backHeight);
  141.            
  142.             //Set speed of each image
  143.             float bspeed = (0.5f - mOffset) * 50.0f;
  144.             float mspeed = (0.5f - mOffset) * 150.0f;
  145.             float fspeed = (0.5f - mOffset) * 400.0f;
  146.            
  147.             c.drawBitmap(_back, bspeed, 0, null);
  148.             c.drawBitmap(_mid, mspeed, 0, null);
  149.             c.drawBitmap(_front, fspeed, 0, null);
  150.            
  151.             c.restore();
  152.         }
  153.     }
  154. }
Parsed in 0.047 seconds, using GeSHi 1.0.8.4

Am I on the right track? Is there a better way? Any input would be appreciated. Thanks for your time.
toebee
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 22, 2010 3:47 am

Top

Language changes

Postby arulelvamj » Thu Jan 06, 2011 7:08 am

hi i am developing widgets and change language settings how to change language for programatically?
arulelvamj
Freshman
Freshman
 
Posts: 5
Joined: Thu Jan 06, 2011 6:30 am

Re: onOffsetsChanged + Parallax Background

Postby toebee » Fri Jan 07, 2011 8:02 pm

Not the reply I was looking for lol
toebee
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 22, 2010 3:47 am

Re: onOffsetsChanged + Parallax Background

Postby graytest » Wed Feb 09, 2011 1:23 pm

The one thing I notice when looking at your code is that you load the bitmaps every frame.
I would load those once in the beginnning, and just reuse the objects.

Let me know if that helps!
User avatar
graytest
Freshman
Freshman
 
Posts: 7
Joined: Tue Feb 01, 2011 9:15 am
Location: Sweden

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests