我正在尝试在android上制作视差动画。一旦解码和其他信息,我正在使用帮助程序类来保存位图图像。视差发生在 ViewPager 类及其 onDraw 方法中。图像已正确缩放,因为这仅适用于一台设备(Galaxy S4)。问题是,如果我按下并返回 ViewPager 足够多次,它会给我一个内存不足错误和崩溃。不知道我做错了什么,有人可以指出我正确的方向。将不胜感激。
注意:我只提交了与位图相关的效果代码,整个类太长了。
// 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();
}
}
}