2

I am trying to make a parallax animation on android. I am using a helper class to hold the bitmap image once its decoded and other information. The parallax is happening inside a ViewPager class and its onDraw method. The images are scaled properly since this is only intended for one device (Galaxy S4). Problem is if I press back and go back to the ViewPager enough times it will give me an out of memory error and crash. Not sure what I am doing wrong, can someone please point me in the right direction. Would be greatly appreciated.

Note: I have only submitted effecting code related to the bitmap, the whole classes are far too long.

// This is the main ViewPager class, also does a bunch of other stuff
public class CustomViewPager extends ViewPager implements OnEditorActionListener,           OnTouchListener {

    private int pageOne = 0, pageTwo = 1, pageThree = 2, pageFour = 3;
    private ViewPageScroller mScroller = null;
    private Context context;
    private int displayWidth = 0;
    private int displayHeight = 0;
    private int position = 0;
    private int offsetPixels = 0;
    private float offset;
    private boolean scrollForward = true;
    private int backEndXOffset = 0;
    private ParallaxBitmap[] bitmap;
    private boolean isPagingEnabled;
    private Paint gettingStartedPaint, gettingStartedInfoMsgPaint;

    private synchronized int getOffsetPixels() {
        return offsetPixels;
    }
    private synchronized void setOffsetPixels(int offset) {
        offsetPixels = offset;
    }

    private synchronized void setPosition(int pos) {
        position = pos;
    }
    private synchronized int getPosition() {
        return position;
    }

    public CustomViewPager(Context context) {
        super(context);
        this.context = context;
        this.setEnabled(true);
        postInitViewPager();
        initDisplayInfo();
        loadBitmaps();
        initPaint();
        initAnimations();
    }
    public void loadBitmaps() {
        int[] parallaxDrawables = new int[] {
            R.drawable.android_payhome,
            R.drawable.arrow_curve_left,
            R.drawable.pay_now,
            R.drawable.machine,
            R.drawable.android_phone_shadow,
            R.drawable.android_phone_paynow,
            R.drawable.sync,
            R.drawable.interac_straighton,
            R.drawable.android_passcode,
            R.drawable.pay_with_passcode,
            R.drawable.arrow_curve_right,
            R.drawable.android_payhome_2
        };
        bitmap = new ParallaxBitmap[parallaxDrawables.length];
        for(int i=0;i<parallaxDrawables.length;i++) {
        bitmap[i] = new ParallaxBitmap(context, parallaxDrawables[i]);
        }
    }
}


// Helper class

public class ParallaxBitmap {
    private Bitmap bitmap = null;
    private int width = 0;
    private int height = 0;
    private int startX = 0;
    private int startY = 0;
    private int endX = 0;
    private int endY = 0;
    private int currentOffsetX = 0;
    private int currentOffsetY = 0;
    private int scrollingSpeed = 0;
    private int currentScrollOffset = 0;
    private int position = 0;

    // Also contains a bunch of setter/getter methods for access.

    public void decodeBitmapResource(int resourceID) {
        InputStream is;     
        try {
            is = context.getResources().openRawResource(resourceID);
            BitmapFactory.Options options = new BitmapFactory.Options();
            //options.inJustDecodeBounds = true;
            bitmap = BitmapFactory.decodeStream(is, null, options);          
            width = options.outWidth;
            height = options.outHeight;
            //options.inJustDecodeBounds = false;
            is.reset();
            is.close(); 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
JasonWyatt
  • 5,275
  • 1
  • 31
  • 39
ahmad
  • 2,149
  • 4
  • 21
  • 38
  • I have a feeling that I need to call recycle on the bitmap image during ViewPager onDestroy or something like that. I will be working on this today and post my results once I have any. I notice others are also looking at this post, so hopefully that helps. – ahmad Sep 09 '13 at 18:05

1 Answers1

0

@ahmad - I had some similar issues, but for customCursorAdapter which uses images. It might help - or at least give you some jumping off points as there are many image related links. See the accepted answer: Android custom SimpleCursorAdapter with image from file with path in database

Community
  • 1
  • 1
Howard Pautz
  • 415
  • 7
  • 21
  • Thanks howard, not sure if this is relevant yet. I will be going through this issue in detail on the weekend and update the answer when I find one. – ahmad Sep 11 '13 at 15:09