mirror of https://github.com/axmolengine/axmol.git
Merge pull request #316 from minggo/iss364
[Android] line break ok on Android
This commit is contained in:
commit
481010a417
|
@ -45,6 +45,7 @@ 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;
|
||||
|
@ -58,6 +59,7 @@ 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;
|
||||
|
@ -72,6 +74,8 @@ 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);
|
||||
|
@ -94,6 +98,11 @@ public class Cocos2dxActivity extends Activity{
|
|||
};
|
||||
}
|
||||
|
||||
public static String getCurrentLanguage() {
|
||||
String languageName = java.util.Locale.getDefault().getLanguage();
|
||||
return languageName;
|
||||
}
|
||||
|
||||
public static void showMessageBox(String title, String message){
|
||||
Message msg = new Message();
|
||||
msg.what = HANDLER_SHOW_DIALOG;
|
||||
|
|
|
@ -8,22 +8,91 @@ import android.graphics.Canvas;
|
|||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Paint.FontMetricsInt;
|
||||
import android.view.Gravity;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class Cocos2dxBitmap{
|
||||
public static void createTextBitmap(String content, int fontSize){
|
||||
/*
|
||||
* The values are the same as cocos2dx/platform/CCImage.h.
|
||||
* I think three alignments are ok.
|
||||
*/
|
||||
private static final int ALIGNCENTER = 0x33;
|
||||
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);
|
||||
|
||||
// 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);
|
||||
|
||||
// draw text to bitmap
|
||||
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height,
|
||||
Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
tv.draw(canvas);
|
||||
|
||||
initNativeObject(bitmap);
|
||||
}
|
||||
|
||||
private static void setTextViewAlignment(TextView tv, int alignment){
|
||||
switch (alignment){
|
||||
case ALIGNCENTER:
|
||||
tv.setGravity(Gravity.CENTER);
|
||||
break;
|
||||
|
||||
case ALIGNLEFT:
|
||||
tv.setGravity(Gravity.LEFT);
|
||||
break;
|
||||
|
||||
case ALIGNRIGHT:
|
||||
tv.setGravity(Gravity.RIGHT);
|
||||
break;
|
||||
|
||||
default:
|
||||
tv.setGravity(Gravity.CENTER);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static class TextProperty{
|
||||
int maxWidth;
|
||||
int height;
|
||||
|
||||
TextProperty(int w, int h){
|
||||
this.maxWidth = w;
|
||||
this.height = h;
|
||||
}
|
||||
}
|
||||
|
||||
private static TextProperty getTextWidthAndHeight(String content, int fontSize){
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(Color.WHITE);
|
||||
paint.setTextSize(fontSize);
|
||||
|
||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
||||
int h = (int)Math.ceil(fm.descent - fm.ascent) + 2;
|
||||
int w = (int)Math.ceil(paint.measureText(content, 0, content.length()));
|
||||
|
||||
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawText(content, 0, h, paint);
|
||||
String[] lines = content.split("\\n");
|
||||
|
||||
initNativeObject(bitmap);
|
||||
/*
|
||||
* Compute the max width
|
||||
*/
|
||||
int w = 0;
|
||||
int temp = 0;
|
||||
for (String line : lines){
|
||||
temp = (int)Math.ceil(paint.measureText(line, 0, line.length()));
|
||||
if (temp > w){
|
||||
w = temp;
|
||||
}
|
||||
}
|
||||
|
||||
return new TextProperty(w, h * lines.length);
|
||||
}
|
||||
|
||||
private static void initNativeObject(Bitmap bitmap){
|
||||
|
|
|
@ -45,6 +45,7 @@ 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;
|
||||
|
@ -58,6 +59,7 @@ 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;
|
||||
|
@ -72,6 +74,8 @@ 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);
|
||||
|
|
|
@ -8,22 +8,91 @@ import android.graphics.Canvas;
|
|||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Paint.FontMetricsInt;
|
||||
import android.view.Gravity;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class Cocos2dxBitmap{
|
||||
public static void createTextBitmap(String content, int fontSize){
|
||||
/*
|
||||
* The values are the same as cocos2dx/platform/CCImage.h.
|
||||
* I think three alignments are ok.
|
||||
*/
|
||||
private static final int ALIGNCENTER = 0x33;
|
||||
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);
|
||||
|
||||
// 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);
|
||||
|
||||
// draw text to bitmap
|
||||
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height,
|
||||
Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
tv.draw(canvas);
|
||||
|
||||
initNativeObject(bitmap);
|
||||
}
|
||||
|
||||
private static void setTextViewAlignment(TextView tv, int alignment){
|
||||
switch (alignment){
|
||||
case ALIGNCENTER:
|
||||
tv.setGravity(Gravity.CENTER);
|
||||
break;
|
||||
|
||||
case ALIGNLEFT:
|
||||
tv.setGravity(Gravity.LEFT);
|
||||
break;
|
||||
|
||||
case ALIGNRIGHT:
|
||||
tv.setGravity(Gravity.RIGHT);
|
||||
break;
|
||||
|
||||
default:
|
||||
tv.setGravity(Gravity.CENTER);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static class TextProperty{
|
||||
int maxWidth;
|
||||
int height;
|
||||
|
||||
TextProperty(int w, int h){
|
||||
this.maxWidth = w;
|
||||
this.height = h;
|
||||
}
|
||||
}
|
||||
|
||||
private static TextProperty getTextWidthAndHeight(String content, int fontSize){
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(Color.WHITE);
|
||||
paint.setTextSize(fontSize);
|
||||
|
||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
||||
int h = (int)Math.ceil(fm.descent - fm.ascent) + 2;
|
||||
int w = (int)Math.ceil(paint.measureText(content, 0, content.length()));
|
||||
|
||||
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawText(content, 0, h, paint);
|
||||
String[] lines = content.split("\\n");
|
||||
|
||||
initNativeObject(bitmap);
|
||||
/*
|
||||
* Compute the max width
|
||||
*/
|
||||
int w = 0;
|
||||
int temp = 0;
|
||||
for (String line : lines){
|
||||
temp = (int)Math.ceil(paint.measureText(line, 0, line.length()));
|
||||
if (temp > w){
|
||||
w = temp;
|
||||
}
|
||||
}
|
||||
|
||||
return new TextProperty(w, h * lines.length);
|
||||
}
|
||||
|
||||
private static void initNativeObject(Bitmap bitmap){
|
||||
|
|
|
@ -73,7 +73,7 @@ public:
|
|||
}
|
||||
|
||||
// get method of createBitmap
|
||||
jmethodID midCreateTextBitmap = env->GetStaticMethodID(mClass, "createTextBitmap", "(Ljava/lang/String;I)V");
|
||||
jmethodID midCreateTextBitmap = env->GetStaticMethodID(mClass, "createTextBitmap", "(Ljava/lang/String;II)V");
|
||||
if (! midCreateTextBitmap)
|
||||
{
|
||||
CCLOG("can not find method createTextBitmap");
|
||||
|
@ -86,7 +86,7 @@ public:
|
|||
* and data.
|
||||
* use this appoach to decrease the jni call number
|
||||
*/
|
||||
env->CallStaticVoidMethod(mClass, midCreateTextBitmap, env->NewStringUTF(text), (int)fontSize);
|
||||
env->CallStaticVoidMethod(mClass, midCreateTextBitmap, env->NewStringUTF(text), (int)fontSize, eAlignMask);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -45,6 +45,7 @@ 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;
|
||||
|
@ -58,6 +59,7 @@ 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;
|
||||
|
@ -72,6 +74,8 @@ 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);
|
||||
|
|
|
@ -8,22 +8,91 @@ import android.graphics.Canvas;
|
|||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Paint.FontMetricsInt;
|
||||
import android.view.Gravity;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class Cocos2dxBitmap{
|
||||
public static void createTextBitmap(String content, int fontSize){
|
||||
/*
|
||||
* The values are the same as cocos2dx/platform/CCImage.h.
|
||||
* I think three alignments are ok.
|
||||
*/
|
||||
private static final int ALIGNCENTER = 0x33;
|
||||
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);
|
||||
|
||||
// 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);
|
||||
|
||||
// draw text to bitmap
|
||||
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height,
|
||||
Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
tv.draw(canvas);
|
||||
|
||||
initNativeObject(bitmap);
|
||||
}
|
||||
|
||||
private static void setTextViewAlignment(TextView tv, int alignment){
|
||||
switch (alignment){
|
||||
case ALIGNCENTER:
|
||||
tv.setGravity(Gravity.CENTER);
|
||||
break;
|
||||
|
||||
case ALIGNLEFT:
|
||||
tv.setGravity(Gravity.LEFT);
|
||||
break;
|
||||
|
||||
case ALIGNRIGHT:
|
||||
tv.setGravity(Gravity.RIGHT);
|
||||
break;
|
||||
|
||||
default:
|
||||
tv.setGravity(Gravity.CENTER);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static class TextProperty{
|
||||
int maxWidth;
|
||||
int height;
|
||||
|
||||
TextProperty(int w, int h){
|
||||
this.maxWidth = w;
|
||||
this.height = h;
|
||||
}
|
||||
}
|
||||
|
||||
private static TextProperty getTextWidthAndHeight(String content, int fontSize){
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(Color.WHITE);
|
||||
paint.setTextSize(fontSize);
|
||||
|
||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
||||
int h = (int)Math.ceil(fm.descent - fm.ascent) + 2;
|
||||
int w = (int)Math.ceil(paint.measureText(content, 0, content.length()));
|
||||
|
||||
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawText(content, 0, h, paint);
|
||||
String[] lines = content.split("\\n");
|
||||
|
||||
initNativeObject(bitmap);
|
||||
/*
|
||||
* Compute the max width
|
||||
*/
|
||||
int w = 0;
|
||||
int temp = 0;
|
||||
for (String line : lines){
|
||||
temp = (int)Math.ceil(paint.measureText(line, 0, line.length()));
|
||||
if (temp > w){
|
||||
w = temp;
|
||||
}
|
||||
}
|
||||
|
||||
return new TextProperty(w, h * lines.length);
|
||||
}
|
||||
|
||||
private static void initNativeObject(Bitmap bitmap){
|
||||
|
|
Loading…
Reference in New Issue