diff --git a/HelloLua/android/AndroidManifest.xml b/HelloLua/android/AndroidManifest.xml index 899435c3e2..313bc777f5 100644 --- a/HelloLua/android/AndroidManifest.xml +++ b/HelloLua/android/AndroidManifest.xml @@ -7,7 +7,8 @@ + android:theme="@android:style/Theme.NoTitleBar.Fullscreen" + android:configChanges="orientation"> diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java index 625013c551..2246055fae 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -45,7 +45,6 @@ package org.cocos2dx.lib; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; -import android.content.Context; import android.content.DialogInterface; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -59,7 +58,6 @@ import android.util.Log; public class Cocos2dxActivity extends Activity{ public static int screenWidth; public static int screenHeight; - public static Context context; private static Cocos2dxMusic backgroundMusicPlayer; private static Cocos2dxSound soundPlayer; private static Cocos2dxAccelerometer accelerometer; @@ -74,8 +72,6 @@ public class Cocos2dxActivity extends Activity{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - context = this; - // get frame size DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index f1a33141ae..5946e8f72b 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -7,9 +7,9 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.graphics.Typeface; +import android.graphics.Paint.Align; import android.graphics.Paint.FontMetricsInt; -import android.view.Gravity; -import android.widget.TextView; public class Cocos2dxBitmap{ /* @@ -20,63 +20,70 @@ public class Cocos2dxBitmap{ private static final int ALIGNLEFT = 0x31; private static final int ALIGNRIGHT = 0x32; - public static void createTextBitmap(String content, int fontSize, int alignment){ - TextProperty textProperty = getTextWidthAndHeight(content, fontSize); + public static void createTextBitmap(String content, String fontName, + int fontSize, int alignment){ + Paint paint = newPaint(fontName, fontSize, alignment); - // create TextView and set corresponding property - TextView tv = new TextView(Cocos2dxActivity.context); - tv.setText(content); - tv.measure(textProperty.maxWidth, textProperty.height); - tv.setTextSize(fontSize); - tv.layout(0, 0, textProperty.maxWidth, textProperty.height); - setTextViewAlignment(tv, alignment); + TextProperty textProperty = getTextWidthAndHeight(content, paint); // draw text to bitmap - Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height, - Bitmap.Config.ARGB_8888); + Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, + textProperty.height * textProperty.numberLines, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); - tv.draw(canvas); + + // draw string + FontMetricsInt fm = paint.getFontMetricsInt(); + int x = 0; + int y = -fm.ascent; + String[] lines = content.split("\\n"); + for (String line : lines){ + x = computeX(paint, line, textProperty.maxWidth, alignment); + canvas.drawText(line, x, y, paint); + y += textProperty.height; + } initNativeObject(bitmap); } - private static void setTextViewAlignment(TextView tv, int alignment){ + private static int computeX(Paint paint, String content, int w, int alignment){ + int ret = 0; + switch (alignment){ case ALIGNCENTER: - tv.setGravity(Gravity.CENTER); + ret = w / 2; break; - - case ALIGNLEFT: - tv.setGravity(Gravity.LEFT); + + // ret = 0 + case ALIGNLEFT: break; case ALIGNRIGHT: - tv.setGravity(Gravity.RIGHT); + ret = w; break; - + + // default is align left ret = 0 default: - tv.setGravity(Gravity.CENTER); break; } + + return ret; } private static class TextProperty{ int maxWidth; int height; + int numberLines; - TextProperty(int w, int h){ + TextProperty(int w, int h, int n){ this.maxWidth = w; this.height = h; + this.numberLines = n; } } - private static TextProperty getTextWidthAndHeight(String content, int fontSize){ - Paint paint = new Paint(); - paint.setColor(Color.WHITE); - paint.setTextSize(fontSize); - + private static TextProperty getTextWidthAndHeight(String content, Paint paint){ FontMetricsInt fm = paint.getFontMetricsInt(); - int h = (int)Math.ceil(fm.descent - fm.ascent) + 2; + int h = (int)Math.ceil(fm.descent - fm.ascent); String[] lines = content.split("\\n"); @@ -92,7 +99,35 @@ public class Cocos2dxBitmap{ } } - return new TextProperty(w, h * lines.length); + return new TextProperty(w, h, lines.length); + } + + private static Paint newPaint(String fontName, int fontSize, int alignment){ + Paint paint = new Paint(); + paint.setColor(Color.WHITE); + paint.setTextSize(fontSize); + paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL)); + paint.setAntiAlias(true); + + switch (alignment){ + case ALIGNCENTER: + paint.setTextAlign(Align.CENTER); + break; + + case ALIGNLEFT: + paint.setTextAlign(Align.LEFT); + break; + + case ALIGNRIGHT: + paint.setTextAlign(Align.RIGHT); + break; + + default: + paint.setTextAlign(Align.LEFT); + break; + } + + return paint; } private static void initNativeObject(Bitmap bitmap){ diff --git a/HelloWorld/android/AndroidManifest.xml b/HelloWorld/android/AndroidManifest.xml index 14f710d119..787e349005 100644 --- a/HelloWorld/android/AndroidManifest.xml +++ b/HelloWorld/android/AndroidManifest.xml @@ -7,7 +7,8 @@ + android:theme="@android:style/Theme.NoTitleBar.Fullscreen" + android:configChanges="orientation"> diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxActivity.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxActivity.java index 625013c551..2246055fae 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -45,7 +45,6 @@ package org.cocos2dx.lib; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; -import android.content.Context; import android.content.DialogInterface; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -59,7 +58,6 @@ import android.util.Log; public class Cocos2dxActivity extends Activity{ public static int screenWidth; public static int screenHeight; - public static Context context; private static Cocos2dxMusic backgroundMusicPlayer; private static Cocos2dxSound soundPlayer; private static Cocos2dxAccelerometer accelerometer; @@ -74,8 +72,6 @@ public class Cocos2dxActivity extends Activity{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - context = this; - // get frame size DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index f1a33141ae..5946e8f72b 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -7,9 +7,9 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.graphics.Typeface; +import android.graphics.Paint.Align; import android.graphics.Paint.FontMetricsInt; -import android.view.Gravity; -import android.widget.TextView; public class Cocos2dxBitmap{ /* @@ -20,63 +20,70 @@ public class Cocos2dxBitmap{ private static final int ALIGNLEFT = 0x31; private static final int ALIGNRIGHT = 0x32; - public static void createTextBitmap(String content, int fontSize, int alignment){ - TextProperty textProperty = getTextWidthAndHeight(content, fontSize); + public static void createTextBitmap(String content, String fontName, + int fontSize, int alignment){ + Paint paint = newPaint(fontName, fontSize, alignment); - // create TextView and set corresponding property - TextView tv = new TextView(Cocos2dxActivity.context); - tv.setText(content); - tv.measure(textProperty.maxWidth, textProperty.height); - tv.setTextSize(fontSize); - tv.layout(0, 0, textProperty.maxWidth, textProperty.height); - setTextViewAlignment(tv, alignment); + TextProperty textProperty = getTextWidthAndHeight(content, paint); // draw text to bitmap - Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height, - Bitmap.Config.ARGB_8888); + Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, + textProperty.height * textProperty.numberLines, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); - tv.draw(canvas); + + // draw string + FontMetricsInt fm = paint.getFontMetricsInt(); + int x = 0; + int y = -fm.ascent; + String[] lines = content.split("\\n"); + for (String line : lines){ + x = computeX(paint, line, textProperty.maxWidth, alignment); + canvas.drawText(line, x, y, paint); + y += textProperty.height; + } initNativeObject(bitmap); } - private static void setTextViewAlignment(TextView tv, int alignment){ + private static int computeX(Paint paint, String content, int w, int alignment){ + int ret = 0; + switch (alignment){ case ALIGNCENTER: - tv.setGravity(Gravity.CENTER); + ret = w / 2; break; - - case ALIGNLEFT: - tv.setGravity(Gravity.LEFT); + + // ret = 0 + case ALIGNLEFT: break; case ALIGNRIGHT: - tv.setGravity(Gravity.RIGHT); + ret = w; break; - + + // default is align left ret = 0 default: - tv.setGravity(Gravity.CENTER); break; } + + return ret; } private static class TextProperty{ int maxWidth; int height; + int numberLines; - TextProperty(int w, int h){ + TextProperty(int w, int h, int n){ this.maxWidth = w; this.height = h; + this.numberLines = n; } } - private static TextProperty getTextWidthAndHeight(String content, int fontSize){ - Paint paint = new Paint(); - paint.setColor(Color.WHITE); - paint.setTextSize(fontSize); - + private static TextProperty getTextWidthAndHeight(String content, Paint paint){ FontMetricsInt fm = paint.getFontMetricsInt(); - int h = (int)Math.ceil(fm.descent - fm.ascent) + 2; + int h = (int)Math.ceil(fm.descent - fm.ascent); String[] lines = content.split("\\n"); @@ -92,7 +99,35 @@ public class Cocos2dxBitmap{ } } - return new TextProperty(w, h * lines.length); + return new TextProperty(w, h, lines.length); + } + + private static Paint newPaint(String fontName, int fontSize, int alignment){ + Paint paint = new Paint(); + paint.setColor(Color.WHITE); + paint.setTextSize(fontSize); + paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL)); + paint.setAntiAlias(true); + + switch (alignment){ + case ALIGNCENTER: + paint.setTextAlign(Align.CENTER); + break; + + case ALIGNLEFT: + paint.setTextAlign(Align.LEFT); + break; + + case ALIGNRIGHT: + paint.setTextAlign(Align.RIGHT); + break; + + default: + paint.setTextAlign(Align.LEFT); + break; + } + + return paint; } private static void initNativeObject(Bitmap bitmap){ diff --git a/cocos2dx/platform/android/CCImage_android.cpp b/cocos2dx/platform/android/CCImage_android.cpp index e85fbd6d93..db55829d56 100644 --- a/cocos2dx/platform/android/CCImage_android.cpp +++ b/cocos2dx/platform/android/CCImage_android.cpp @@ -73,7 +73,7 @@ public: } // get method of createBitmap - jmethodID midCreateTextBitmap = env->GetStaticMethodID(mClass, "createTextBitmap", "(Ljava/lang/String;II)V"); + jmethodID midCreateTextBitmap = env->GetStaticMethodID(mClass, "createTextBitmap", "(Ljava/lang/String;Ljava/lang/String;II)V"); if (! midCreateTextBitmap) { CCLOG("can not find method createTextBitmap"); @@ -86,7 +86,8 @@ public: * and data. * use this appoach to decrease the jni call number */ - env->CallStaticVoidMethod(mClass, midCreateTextBitmap, env->NewStringUTF(text), (int)fontSize, eAlignMask); + env->CallStaticVoidMethod(mClass, midCreateTextBitmap, env->NewStringUTF(text), env->NewStringUTF(pFontName), + (int)fontSize, eAlignMask); return true; } diff --git a/tests/test.android/AndroidManifest.xml b/tests/test.android/AndroidManifest.xml index 17e29c7d40..8b5cdddc21 100644 --- a/tests/test.android/AndroidManifest.xml +++ b/tests/test.android/AndroidManifest.xml @@ -7,7 +7,8 @@ + android:theme="@android:style/Theme.NoTitleBar.Fullscreen" + android:configChanges="orientation"> diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java index 625013c551..2246055fae 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -45,7 +45,6 @@ package org.cocos2dx.lib; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; -import android.content.Context; import android.content.DialogInterface; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -59,7 +58,6 @@ import android.util.Log; public class Cocos2dxActivity extends Activity{ public static int screenWidth; public static int screenHeight; - public static Context context; private static Cocos2dxMusic backgroundMusicPlayer; private static Cocos2dxSound soundPlayer; private static Cocos2dxAccelerometer accelerometer; @@ -74,8 +72,6 @@ public class Cocos2dxActivity extends Activity{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - context = this; - // get frame size DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index f1a33141ae..5946e8f72b 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -7,9 +7,9 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.graphics.Typeface; +import android.graphics.Paint.Align; import android.graphics.Paint.FontMetricsInt; -import android.view.Gravity; -import android.widget.TextView; public class Cocos2dxBitmap{ /* @@ -20,63 +20,70 @@ public class Cocos2dxBitmap{ private static final int ALIGNLEFT = 0x31; private static final int ALIGNRIGHT = 0x32; - public static void createTextBitmap(String content, int fontSize, int alignment){ - TextProperty textProperty = getTextWidthAndHeight(content, fontSize); + public static void createTextBitmap(String content, String fontName, + int fontSize, int alignment){ + Paint paint = newPaint(fontName, fontSize, alignment); - // create TextView and set corresponding property - TextView tv = new TextView(Cocos2dxActivity.context); - tv.setText(content); - tv.measure(textProperty.maxWidth, textProperty.height); - tv.setTextSize(fontSize); - tv.layout(0, 0, textProperty.maxWidth, textProperty.height); - setTextViewAlignment(tv, alignment); + TextProperty textProperty = getTextWidthAndHeight(content, paint); // draw text to bitmap - Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height, - Bitmap.Config.ARGB_8888); + Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, + textProperty.height * textProperty.numberLines, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); - tv.draw(canvas); + + // draw string + FontMetricsInt fm = paint.getFontMetricsInt(); + int x = 0; + int y = -fm.ascent; + String[] lines = content.split("\\n"); + for (String line : lines){ + x = computeX(paint, line, textProperty.maxWidth, alignment); + canvas.drawText(line, x, y, paint); + y += textProperty.height; + } initNativeObject(bitmap); } - private static void setTextViewAlignment(TextView tv, int alignment){ + private static int computeX(Paint paint, String content, int w, int alignment){ + int ret = 0; + switch (alignment){ case ALIGNCENTER: - tv.setGravity(Gravity.CENTER); + ret = w / 2; break; - - case ALIGNLEFT: - tv.setGravity(Gravity.LEFT); + + // ret = 0 + case ALIGNLEFT: break; case ALIGNRIGHT: - tv.setGravity(Gravity.RIGHT); + ret = w; break; - + + // default is align left ret = 0 default: - tv.setGravity(Gravity.CENTER); break; } + + return ret; } private static class TextProperty{ int maxWidth; int height; + int numberLines; - TextProperty(int w, int h){ + TextProperty(int w, int h, int n){ this.maxWidth = w; this.height = h; + this.numberLines = n; } } - private static TextProperty getTextWidthAndHeight(String content, int fontSize){ - Paint paint = new Paint(); - paint.setColor(Color.WHITE); - paint.setTextSize(fontSize); - + private static TextProperty getTextWidthAndHeight(String content, Paint paint){ FontMetricsInt fm = paint.getFontMetricsInt(); - int h = (int)Math.ceil(fm.descent - fm.ascent) + 2; + int h = (int)Math.ceil(fm.descent - fm.ascent); String[] lines = content.split("\\n"); @@ -92,7 +99,35 @@ public class Cocos2dxBitmap{ } } - return new TextProperty(w, h * lines.length); + return new TextProperty(w, h, lines.length); + } + + private static Paint newPaint(String fontName, int fontSize, int alignment){ + Paint paint = new Paint(); + paint.setColor(Color.WHITE); + paint.setTextSize(fontSize); + paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL)); + paint.setAntiAlias(true); + + switch (alignment){ + case ALIGNCENTER: + paint.setTextAlign(Align.CENTER); + break; + + case ALIGNLEFT: + paint.setTextAlign(Align.LEFT); + break; + + case ALIGNRIGHT: + paint.setTextAlign(Align.RIGHT); + break; + + default: + paint.setTextAlign(Align.LEFT); + break; + } + + return paint; } private static void initNativeObject(Bitmap bitmap){