Skip to content

Commit

Permalink
Merge pull request #7 from m1noon/recycled_bmp
Browse files Browse the repository at this point in the history
Stop drawing if bmp has been recycled or DisplayBase is removed.
  • Loading branch information
MasayukiSuda committed Feb 8, 2016
2 parents 686e223 + 7137ca9 commit dfd84b9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
12 changes: 8 additions & 4 deletions library/src/main/java/com/daasuu/library/Container.java
Expand Up @@ -99,11 +99,12 @@ public Container addChildAt(int location, @NonNull DisplayObject DisplayObject)
/**
* Removes the specified child from the display list.
*
* @param DisplayObject DisplayObject
* @param displayObject DisplayObject
* @return this
*/
public Container removeChild(@NonNull DisplayObject DisplayObject) {
mDisplayList.remove(DisplayObject);
public Container removeChild(@NonNull DisplayObject displayObject) {
displayObject.disable();
mDisplayList.remove(displayObject);
return this;
}

Expand All @@ -114,7 +115,10 @@ public Container removeChild(@NonNull DisplayObject DisplayObject) {
* @return this
*/
public Container removeChildAt(int location) {
mDisplayList.remove(location);
DisplayBase removed = mDisplayList.remove(location);
if (removed != null) {
removed.disable();
}
return this;
}

Expand Down
25 changes: 22 additions & 3 deletions library/src/main/java/com/daasuu/library/DisplayBase.java
Expand Up @@ -20,12 +20,17 @@ public abstract class DisplayBase {

protected Drawer mDrawer;

private boolean mIsEnable;

/**
* call from FPSTextureView or FPSSurfaceView when it is addChild.
*
* @param fps Set in FPSTextureView or FPSSurfaceView.
*/
void setUp(long fps) {
synchronized (this) {
mIsEnable = true;
}
mAnimator.setUp(fps);
}

Expand Down Expand Up @@ -60,9 +65,13 @@ public DisplayBase animator(@NonNull Animator animator) {
* @param canvas This Canvas acquired by lookCanvas in FPSTextureView or FPSSurfaceView.
*/
void draw(@NonNull Canvas canvas) {
mAnimator.setBaseLine(canvas, mDrawer.getWidth(), mDrawer.getHeight());
mAnimator.updateAnimParam(mAnimParameter);
mDrawer.draw(canvas, mAnimParameter.x, mAnimParameter.y, mAnimParameter.alpha, mAnimParameter.scaleX, mAnimParameter.scaleY, mAnimParameter.rotation);
synchronized (this) {
if (mIsEnable) {
mAnimator.setBaseLine(canvas, mDrawer.getWidth(), mDrawer.getHeight());
mAnimator.updateAnimParam(mAnimParameter);
mDrawer.draw(canvas, mAnimParameter.x, mAnimParameter.y, mAnimParameter.alpha, mAnimParameter.scaleX, mAnimParameter.scaleY, mAnimParameter.rotation);
}
}
}

public AnimParameter getAnimParameter() {
Expand All @@ -87,6 +96,16 @@ public boolean isPause() {
return mAnimator.isPause();
}

/**
* disable this display object.
* This call from FPSTextureView, FPSSurfaceView or Container when it is removeChild.
*/
void disable() {
synchronized (this) {
mIsEnable = false;
}
}

/**
* Composer provide simple composing interface.
*/
Expand Down
12 changes: 8 additions & 4 deletions library/src/main/java/com/daasuu/library/FPSSurfaceView.java
Expand Up @@ -162,11 +162,12 @@ public FPSSurfaceView addChildAt(int location, @NonNull DisplayBase DisplayBase)
/**
* Removes the specified child from the display list.
*
* @param DisplayBase DisplayBase2
* @param displayBase DisplayBase2
* @return this
*/
public FPSSurfaceView removeChild(@NonNull DisplayBase DisplayBase) {
mDisplayList.remove(DisplayBase);
public FPSSurfaceView removeChild(@NonNull DisplayBase displayBase) {
displayBase.disable();
mDisplayList.remove(displayBase);
return this;
}

Expand All @@ -177,7 +178,10 @@ public FPSSurfaceView removeChild(@NonNull DisplayBase DisplayBase) {
* @return this
*/
public FPSSurfaceView removeChildAt(int location) {
mDisplayList.remove(location);
DisplayBase removed = mDisplayList.remove(location);
if (removed != null) {
removed.disable();
}
return this;
}

Expand Down
6 changes: 5 additions & 1 deletion library/src/main/java/com/daasuu/library/FPSTextureView.java
Expand Up @@ -170,6 +170,7 @@ public FPSTextureView addChildAt(int location, @NonNull DisplayBase displayBase)
* @return this
*/
public FPSTextureView removeChild(@NonNull DisplayBase displayBase) {
displayBase.disable();
boolean a = mDisplayList.remove(displayBase);
return this;
}
Expand All @@ -181,7 +182,10 @@ public FPSTextureView removeChild(@NonNull DisplayBase displayBase) {
* @return this
*/
public FPSTextureView removeChildAt(int location) {
mDisplayList.remove(location);
DisplayBase removed = mDisplayList.remove(location);
if (removed != null) {
removed.disable();
}
return this;
}

Expand Down
Expand Up @@ -112,6 +112,11 @@ protected void draw(Canvas canvas, float x, float y) {
return;
}

if (mBitmap.isRecycled()) {
mBitmap = null;
return;
}

if (mDpSize) {
mDpSizeRect.set(
x,
Expand Down
Expand Up @@ -230,6 +230,11 @@ public boolean isSpritePause() {
protected void draw(Canvas canvas, float x, float y) {
if (mBitmap == null) return;

if (mBitmap.isRecycled()) {
mBitmap = null;
return;
}

updateSpriteFrame();
mBitmapRect.set((int) (mSpriteSheet.dx), (int) (mSpriteSheet.dy), (int) (mSpriteSheet.dx + mSpriteSheet.frameWidth), (int) (mSpriteSheet.dy + mSpriteSheet.frameHeight));

Expand Down

0 comments on commit dfd84b9

Please sign in to comment.