Skip to content

Commit

Permalink
Merge pull request #6 from m1noon/reduce_memory_usage
Browse files Browse the repository at this point in the history
Reduce memory usage
  • Loading branch information
MasayukiSuda committed Feb 8, 2016
2 parents ba82782 + 16df286 commit 686e223
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
7 changes: 4 additions & 3 deletions library/src/main/java/com/daasuu/library/FPSSurfaceView.java
Expand Up @@ -29,6 +29,7 @@ public class FPSSurfaceView extends SurfaceView implements SurfaceHolder.Callbac
private SurfaceHolder mSurfaceHolder;

private List<DisplayBase> mDisplayList = new ArrayList<>();
private final List<DisplayBase> mDrawingList = new ArrayList<>();

public FPSSurfaceView(Context context) {
this(context, null, 0);
Expand Down Expand Up @@ -99,19 +100,19 @@ private void onTick() {

synchronized (this) {

List<DisplayBase> copyDisplayBaseList = new ArrayList<DisplayBase>(mDisplayList);

Canvas canvas = mSurfaceHolder.lockCanvas();
if (canvas == null) return;

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

for (DisplayBase DisplayBase : copyDisplayBaseList) {
mDrawingList.addAll(mDisplayList);
for (DisplayBase DisplayBase : mDrawingList) {
if (DisplayBase == null) {
continue;
}
DisplayBase.draw(canvas);
}
mDrawingList.clear();

mSurfaceHolder.unlockCanvasAndPost(canvas);
}
Expand Down
8 changes: 4 additions & 4 deletions library/src/main/java/com/daasuu/library/FPSTextureView.java
Expand Up @@ -25,6 +25,7 @@ public class FPSTextureView extends TextureView implements TextureView.SurfaceTe
private int mFps = Constant.DEFAULT_FPS;

private List<DisplayBase> mDisplayList = new ArrayList<>();
private final List<DisplayBase> mDrawingList = new ArrayList<>();

public FPSTextureView(Context context) {
this(context, null, 0);
Expand Down Expand Up @@ -90,20 +91,19 @@ public void tickStop() {
private void onTick() {

synchronized (this) {

List<DisplayBase> copyDisplayObjectList = new ArrayList<DisplayBase>(mDisplayList);

Canvas canvas = this.lockCanvas();
if (canvas == null) return;

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

for (DisplayBase displayBase : copyDisplayObjectList) {
mDrawingList.addAll(mDisplayList);
for (DisplayBase displayBase : mDrawingList) {
if (displayBase == null) {
continue;
}
displayBase.draw(canvas);
}
mDrawingList.clear();

this.unlockCanvasAndPost(canvas);
}
Expand Down
Expand Up @@ -41,6 +41,11 @@ public class BitmapDrawer extends BaseDrawer {
*/
private Rect mBitmapRect;

/**
* The rectangle that the bitmap will be scaled/translated to fit into
*/
private RectF mDpSizeRect;

public BitmapDrawer(@NonNull Bitmap bitmap) {
super(new Paint());
this.mBitmap = bitmap;
Expand All @@ -57,6 +62,7 @@ public BitmapDrawer dpSize(@NonNull Context context) {
mBitmapDpWidth = Util.convertPixelsToDp(mBitmap.getWidth(), context);
mBitmapDpHeight = Util.convertPixelsToDp(mBitmap.getHeight(), context);
mBitmapRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
mDpSizeRect = new RectF();
return this;
}

Expand Down Expand Up @@ -107,13 +113,13 @@ protected void draw(Canvas canvas, float x, float y) {
}

if (mDpSize) {
RectF dpSizeRect = new RectF(
mDpSizeRect.set(
x,
y,
x + mBitmapDpWidth,
y + mBitmapDpHeight
);
canvas.drawBitmap(mBitmap, mBitmapRect, dpSizeRect, mPaint);
canvas.drawBitmap(mBitmap, mBitmapRect, mDpSizeRect, mPaint);
} else {
canvas.drawBitmap(mBitmap, x, y, mPaint);
}
Expand Down
Expand Up @@ -44,6 +44,16 @@ public class SpriteSheetDrawer extends BaseDrawer {
*/
private SpriteSheet mSpriteSheet;

/**
* The subset of the SpriteSheet bitmap to be drawn
*/
private final Rect mBitmapRect;

/**
* The rectangle that the SpriteSheet bitmap will be scaled/translated to fit into
*/
private final Rect mBounds;

/**
* member variables needed to skip a tick in mFrequency
*/
Expand Down Expand Up @@ -96,6 +106,8 @@ public SpriteSheetDrawer(@NonNull Bitmap bitmap, @NonNull SpriteSheet spriteShee
super(new Paint());
this.mBitmap = bitmap;
this.mSpriteSheet = spriteSheet;
mBitmapRect = new Rect();
mBounds = new Rect();
}


Expand Down Expand Up @@ -219,27 +231,27 @@ protected void draw(Canvas canvas, float x, float y) {
if (mBitmap == null) return;

updateSpriteFrame();
Rect bitmapRect = new Rect((int) (mSpriteSheet.dx), (int) (mSpriteSheet.dy), (int) (mSpriteSheet.dx + mSpriteSheet.frameWidth), (int) (mSpriteSheet.dy + mSpriteSheet.frameHeight));
mBitmapRect.set((int) (mSpriteSheet.dx), (int) (mSpriteSheet.dy), (int) (mSpriteSheet.dx + mSpriteSheet.frameWidth), (int) (mSpriteSheet.dy + mSpriteSheet.frameHeight));

if (mDpSize) {

Rect bounds = new Rect(
mBounds.set(
(int) x,
(int) y,
(int) (x + mBitmapDpWidth),
(int) (y + mBitmapDpHeight)
);
canvas.drawBitmap(mBitmap, bitmapRect, bounds, mPaint);
canvas.drawBitmap(mBitmap, mBitmapRect, mBounds, mPaint);

} else {

Rect bounds = new Rect(
mBounds.set(
(int) x,
(int) y,
(int) (x + mSpriteSheet.frameWidth),
(int) (y + mSpriteSheet.frameHeight)
);
canvas.drawBitmap(mBitmap, bitmapRect, bounds, mPaint);
canvas.drawBitmap(mBitmap, mBitmapRect, mBounds, mPaint);
}

}
Expand Down

0 comments on commit 686e223

Please sign in to comment.