mirror of https://github.com/axmolengine/axmol.git
[android] fixed #364: show string correct & line break ok
This commit is contained in:
parent
a676e0d401
commit
8440d6a267
|
@ -7,7 +7,8 @@
|
|||
<activity android:name=".HelloLua"
|
||||
android:label="@string/app_name"
|
||||
android:screenOrientation="landscape"
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
|
||||
android:configChanges="orientation">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
// ret = 0
|
||||
case ALIGNLEFT:
|
||||
tv.setGravity(Gravity.LEFT);
|
||||
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){
|
||||
|
|
|
@ -7,7 +7,8 @@
|
|||
<activity android:name=".ApplicationDemo"
|
||||
android:label="@string/app_name"
|
||||
android:screenOrientation="landscape"
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
|
||||
android:configChanges="orientation">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
// ret = 0
|
||||
case ALIGNLEFT:
|
||||
tv.setGravity(Gravity.LEFT);
|
||||
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){
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -7,7 +7,8 @@
|
|||
<activity android:name=".TestsDemo"
|
||||
android:label="@string/app_name"
|
||||
android:screenOrientation="landscape"
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
|
||||
android:configChanges="orientation">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
// ret = 0
|
||||
case ALIGNLEFT:
|
||||
tv.setGravity(Gravity.LEFT);
|
||||
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){
|
||||
|
|
Loading…
Reference in New Issue