From dd8a2deeafe22aefdefcec78ccaced12ee2c0cef Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 16 Nov 2011 11:40:24 +0800 Subject: [PATCH] fixed #758: fix the bug that TTF fonts cause low memory on android --- .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 3 +- .../org/cocos2dx/lib/Cocos2dxTypefaces.java | 44 +++++++++++++++++++ .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 3 +- .../org/cocos2dx/lib/Cocos2dxTypefaces.java | 44 +++++++++++++++++++ .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 3 +- .../org/cocos2dx/lib/Cocos2dxTypefaces.java | 44 +++++++++++++++++++ 6 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 HelloLua/android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java create mode 100644 HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java create mode 100644 tests/test.android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 9a2ddadc65..62e2904f78 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -284,7 +284,8 @@ public class Cocos2dxBitmap{ */ if (fontName.endsWith(".ttf")){ try { - Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName); + //Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName); + Typeface typeFace = Cocos2dxTypefaces.get(context, fontName); paint.setTypeface(typeFace); } catch (Exception e){ Log.e("Cocos2dxBitmap", diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java new file mode 100644 index 0000000000..79af1ed3af --- /dev/null +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java @@ -0,0 +1,44 @@ +/**************************************************************************** +Copyright (c) 2010-2011 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +package org.cocos2dx.lib; + +import java.util.Hashtable; + +import android.content.Context; +import android.graphics.Typeface; + +public class Cocos2dxTypefaces { + private static final Hashtable cache = new Hashtable(); + + public static Typeface get(Context context, String name){ + synchronized(cache){ + if (! cache.containsKey(name)){ + Typeface t = Typeface.createFromAsset(context.getAssets(), name); + cache.put(name, t); + } + + return cache.get(name); + } + } +} diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 9a2ddadc65..62e2904f78 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -284,7 +284,8 @@ public class Cocos2dxBitmap{ */ if (fontName.endsWith(".ttf")){ try { - Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName); + //Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName); + Typeface typeFace = Cocos2dxTypefaces.get(context, fontName); paint.setTypeface(typeFace); } catch (Exception e){ Log.e("Cocos2dxBitmap", diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java new file mode 100644 index 0000000000..79af1ed3af --- /dev/null +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java @@ -0,0 +1,44 @@ +/**************************************************************************** +Copyright (c) 2010-2011 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +package org.cocos2dx.lib; + +import java.util.Hashtable; + +import android.content.Context; +import android.graphics.Typeface; + +public class Cocos2dxTypefaces { + private static final Hashtable cache = new Hashtable(); + + public static Typeface get(Context context, String name){ + synchronized(cache){ + if (! cache.containsKey(name)){ + Typeface t = Typeface.createFromAsset(context.getAssets(), name); + cache.put(name, t); + } + + return cache.get(name); + } + } +} diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 9a2ddadc65..62e2904f78 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -284,7 +284,8 @@ public class Cocos2dxBitmap{ */ if (fontName.endsWith(".ttf")){ try { - Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName); + //Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName); + Typeface typeFace = Cocos2dxTypefaces.get(context, fontName); paint.setTypeface(typeFace); } catch (Exception e){ Log.e("Cocos2dxBitmap", diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java new file mode 100644 index 0000000000..79af1ed3af --- /dev/null +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxTypefaces.java @@ -0,0 +1,44 @@ +/**************************************************************************** +Copyright (c) 2010-2011 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +package org.cocos2dx.lib; + +import java.util.Hashtable; + +import android.content.Context; +import android.graphics.Typeface; + +public class Cocos2dxTypefaces { + private static final Hashtable cache = new Hashtable(); + + public static Typeface get(Context context, String name){ + synchronized(cache){ + if (! cache.containsKey(name)){ + Typeface t = Typeface.createFromAsset(context.getAssets(), name); + cache.put(name, t); + } + + return cache.get(name); + } + } +}