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"
|
<activity android:name=".HelloLua"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:screenOrientation="landscape"
|
android:screenOrientation="landscape"
|
||||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
|
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
|
||||||
|
android:configChanges="orientation">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
|
|
@ -45,7 +45,6 @@ package org.cocos2dx.lib;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
@ -59,7 +58,6 @@ import android.util.Log;
|
||||||
public class Cocos2dxActivity extends Activity{
|
public class Cocos2dxActivity extends Activity{
|
||||||
public static int screenWidth;
|
public static int screenWidth;
|
||||||
public static int screenHeight;
|
public static int screenHeight;
|
||||||
public static Context context;
|
|
||||||
private static Cocos2dxMusic backgroundMusicPlayer;
|
private static Cocos2dxMusic backgroundMusicPlayer;
|
||||||
private static Cocos2dxSound soundPlayer;
|
private static Cocos2dxSound soundPlayer;
|
||||||
private static Cocos2dxAccelerometer accelerometer;
|
private static Cocos2dxAccelerometer accelerometer;
|
||||||
|
@ -74,8 +72,6 @@ public class Cocos2dxActivity extends Activity{
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
context = this;
|
|
||||||
|
|
||||||
// get frame size
|
// get frame size
|
||||||
DisplayMetrics dm = new DisplayMetrics();
|
DisplayMetrics dm = new DisplayMetrics();
|
||||||
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
||||||
|
|
|
@ -7,9 +7,9 @@ import android.graphics.Bitmap;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
import android.graphics.Typeface;
|
||||||
|
import android.graphics.Paint.Align;
|
||||||
import android.graphics.Paint.FontMetricsInt;
|
import android.graphics.Paint.FontMetricsInt;
|
||||||
import android.view.Gravity;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
public class Cocos2dxBitmap{
|
public class Cocos2dxBitmap{
|
||||||
/*
|
/*
|
||||||
|
@ -20,63 +20,70 @@ public class Cocos2dxBitmap{
|
||||||
private static final int ALIGNLEFT = 0x31;
|
private static final int ALIGNLEFT = 0x31;
|
||||||
private static final int ALIGNRIGHT = 0x32;
|
private static final int ALIGNRIGHT = 0x32;
|
||||||
|
|
||||||
public static void createTextBitmap(String content, int fontSize, int alignment){
|
public static void createTextBitmap(String content, String fontName,
|
||||||
TextProperty textProperty = getTextWidthAndHeight(content, fontSize);
|
int fontSize, int alignment){
|
||||||
|
Paint paint = newPaint(fontName, fontSize, alignment);
|
||||||
|
|
||||||
// create TextView and set corresponding property
|
TextProperty textProperty = getTextWidthAndHeight(content, paint);
|
||||||
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
|
// draw text to bitmap
|
||||||
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height,
|
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth,
|
||||||
Bitmap.Config.ARGB_8888);
|
textProperty.height * textProperty.numberLines, Bitmap.Config.ARGB_8888);
|
||||||
Canvas canvas = new Canvas(bitmap);
|
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);
|
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){
|
switch (alignment){
|
||||||
case ALIGNCENTER:
|
case ALIGNCENTER:
|
||||||
tv.setGravity(Gravity.CENTER);
|
ret = w / 2;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ALIGNLEFT:
|
// ret = 0
|
||||||
tv.setGravity(Gravity.LEFT);
|
case ALIGNLEFT:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ALIGNRIGHT:
|
case ALIGNRIGHT:
|
||||||
tv.setGravity(Gravity.RIGHT);
|
ret = w;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// default is align left ret = 0
|
||||||
default:
|
default:
|
||||||
tv.setGravity(Gravity.CENTER);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TextProperty{
|
private static class TextProperty{
|
||||||
int maxWidth;
|
int maxWidth;
|
||||||
int height;
|
int height;
|
||||||
|
int numberLines;
|
||||||
|
|
||||||
TextProperty(int w, int h){
|
TextProperty(int w, int h, int n){
|
||||||
this.maxWidth = w;
|
this.maxWidth = w;
|
||||||
this.height = h;
|
this.height = h;
|
||||||
|
this.numberLines = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TextProperty getTextWidthAndHeight(String content, int fontSize){
|
private static TextProperty getTextWidthAndHeight(String content, Paint paint){
|
||||||
Paint paint = new Paint();
|
|
||||||
paint.setColor(Color.WHITE);
|
|
||||||
paint.setTextSize(fontSize);
|
|
||||||
|
|
||||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
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");
|
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){
|
private static void initNativeObject(Bitmap bitmap){
|
||||||
|
|
|
@ -7,7 +7,8 @@
|
||||||
<activity android:name=".ApplicationDemo"
|
<activity android:name=".ApplicationDemo"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:screenOrientation="landscape"
|
android:screenOrientation="landscape"
|
||||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
|
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
|
||||||
|
android:configChanges="orientation">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
|
|
@ -45,7 +45,6 @@ package org.cocos2dx.lib;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
@ -59,7 +58,6 @@ import android.util.Log;
|
||||||
public class Cocos2dxActivity extends Activity{
|
public class Cocos2dxActivity extends Activity{
|
||||||
public static int screenWidth;
|
public static int screenWidth;
|
||||||
public static int screenHeight;
|
public static int screenHeight;
|
||||||
public static Context context;
|
|
||||||
private static Cocos2dxMusic backgroundMusicPlayer;
|
private static Cocos2dxMusic backgroundMusicPlayer;
|
||||||
private static Cocos2dxSound soundPlayer;
|
private static Cocos2dxSound soundPlayer;
|
||||||
private static Cocos2dxAccelerometer accelerometer;
|
private static Cocos2dxAccelerometer accelerometer;
|
||||||
|
@ -74,8 +72,6 @@ public class Cocos2dxActivity extends Activity{
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
context = this;
|
|
||||||
|
|
||||||
// get frame size
|
// get frame size
|
||||||
DisplayMetrics dm = new DisplayMetrics();
|
DisplayMetrics dm = new DisplayMetrics();
|
||||||
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
||||||
|
|
|
@ -7,9 +7,9 @@ import android.graphics.Bitmap;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
import android.graphics.Typeface;
|
||||||
|
import android.graphics.Paint.Align;
|
||||||
import android.graphics.Paint.FontMetricsInt;
|
import android.graphics.Paint.FontMetricsInt;
|
||||||
import android.view.Gravity;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
public class Cocos2dxBitmap{
|
public class Cocos2dxBitmap{
|
||||||
/*
|
/*
|
||||||
|
@ -20,63 +20,70 @@ public class Cocos2dxBitmap{
|
||||||
private static final int ALIGNLEFT = 0x31;
|
private static final int ALIGNLEFT = 0x31;
|
||||||
private static final int ALIGNRIGHT = 0x32;
|
private static final int ALIGNRIGHT = 0x32;
|
||||||
|
|
||||||
public static void createTextBitmap(String content, int fontSize, int alignment){
|
public static void createTextBitmap(String content, String fontName,
|
||||||
TextProperty textProperty = getTextWidthAndHeight(content, fontSize);
|
int fontSize, int alignment){
|
||||||
|
Paint paint = newPaint(fontName, fontSize, alignment);
|
||||||
|
|
||||||
// create TextView and set corresponding property
|
TextProperty textProperty = getTextWidthAndHeight(content, paint);
|
||||||
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
|
// draw text to bitmap
|
||||||
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height,
|
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth,
|
||||||
Bitmap.Config.ARGB_8888);
|
textProperty.height * textProperty.numberLines, Bitmap.Config.ARGB_8888);
|
||||||
Canvas canvas = new Canvas(bitmap);
|
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);
|
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){
|
switch (alignment){
|
||||||
case ALIGNCENTER:
|
case ALIGNCENTER:
|
||||||
tv.setGravity(Gravity.CENTER);
|
ret = w / 2;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ALIGNLEFT:
|
// ret = 0
|
||||||
tv.setGravity(Gravity.LEFT);
|
case ALIGNLEFT:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ALIGNRIGHT:
|
case ALIGNRIGHT:
|
||||||
tv.setGravity(Gravity.RIGHT);
|
ret = w;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// default is align left ret = 0
|
||||||
default:
|
default:
|
||||||
tv.setGravity(Gravity.CENTER);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TextProperty{
|
private static class TextProperty{
|
||||||
int maxWidth;
|
int maxWidth;
|
||||||
int height;
|
int height;
|
||||||
|
int numberLines;
|
||||||
|
|
||||||
TextProperty(int w, int h){
|
TextProperty(int w, int h, int n){
|
||||||
this.maxWidth = w;
|
this.maxWidth = w;
|
||||||
this.height = h;
|
this.height = h;
|
||||||
|
this.numberLines = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TextProperty getTextWidthAndHeight(String content, int fontSize){
|
private static TextProperty getTextWidthAndHeight(String content, Paint paint){
|
||||||
Paint paint = new Paint();
|
|
||||||
paint.setColor(Color.WHITE);
|
|
||||||
paint.setTextSize(fontSize);
|
|
||||||
|
|
||||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
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");
|
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){
|
private static void initNativeObject(Bitmap bitmap){
|
||||||
|
|
|
@ -73,7 +73,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// get method of createBitmap
|
// 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)
|
if (! midCreateTextBitmap)
|
||||||
{
|
{
|
||||||
CCLOG("can not find method createTextBitmap");
|
CCLOG("can not find method createTextBitmap");
|
||||||
|
@ -86,7 +86,8 @@ public:
|
||||||
* and data.
|
* and data.
|
||||||
* use this appoach to decrease the jni call number
|
* 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,8 @@
|
||||||
<activity android:name=".TestsDemo"
|
<activity android:name=".TestsDemo"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:screenOrientation="landscape"
|
android:screenOrientation="landscape"
|
||||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
|
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
|
||||||
|
android:configChanges="orientation">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
|
|
@ -45,7 +45,6 @@ package org.cocos2dx.lib;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
@ -59,7 +58,6 @@ import android.util.Log;
|
||||||
public class Cocos2dxActivity extends Activity{
|
public class Cocos2dxActivity extends Activity{
|
||||||
public static int screenWidth;
|
public static int screenWidth;
|
||||||
public static int screenHeight;
|
public static int screenHeight;
|
||||||
public static Context context;
|
|
||||||
private static Cocos2dxMusic backgroundMusicPlayer;
|
private static Cocos2dxMusic backgroundMusicPlayer;
|
||||||
private static Cocos2dxSound soundPlayer;
|
private static Cocos2dxSound soundPlayer;
|
||||||
private static Cocos2dxAccelerometer accelerometer;
|
private static Cocos2dxAccelerometer accelerometer;
|
||||||
|
@ -74,8 +72,6 @@ public class Cocos2dxActivity extends Activity{
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
context = this;
|
|
||||||
|
|
||||||
// get frame size
|
// get frame size
|
||||||
DisplayMetrics dm = new DisplayMetrics();
|
DisplayMetrics dm = new DisplayMetrics();
|
||||||
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
||||||
|
|
|
@ -7,9 +7,9 @@ import android.graphics.Bitmap;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
import android.graphics.Typeface;
|
||||||
|
import android.graphics.Paint.Align;
|
||||||
import android.graphics.Paint.FontMetricsInt;
|
import android.graphics.Paint.FontMetricsInt;
|
||||||
import android.view.Gravity;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
public class Cocos2dxBitmap{
|
public class Cocos2dxBitmap{
|
||||||
/*
|
/*
|
||||||
|
@ -20,63 +20,70 @@ public class Cocos2dxBitmap{
|
||||||
private static final int ALIGNLEFT = 0x31;
|
private static final int ALIGNLEFT = 0x31;
|
||||||
private static final int ALIGNRIGHT = 0x32;
|
private static final int ALIGNRIGHT = 0x32;
|
||||||
|
|
||||||
public static void createTextBitmap(String content, int fontSize, int alignment){
|
public static void createTextBitmap(String content, String fontName,
|
||||||
TextProperty textProperty = getTextWidthAndHeight(content, fontSize);
|
int fontSize, int alignment){
|
||||||
|
Paint paint = newPaint(fontName, fontSize, alignment);
|
||||||
|
|
||||||
// create TextView and set corresponding property
|
TextProperty textProperty = getTextWidthAndHeight(content, paint);
|
||||||
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
|
// draw text to bitmap
|
||||||
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height,
|
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth,
|
||||||
Bitmap.Config.ARGB_8888);
|
textProperty.height * textProperty.numberLines, Bitmap.Config.ARGB_8888);
|
||||||
Canvas canvas = new Canvas(bitmap);
|
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);
|
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){
|
switch (alignment){
|
||||||
case ALIGNCENTER:
|
case ALIGNCENTER:
|
||||||
tv.setGravity(Gravity.CENTER);
|
ret = w / 2;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ALIGNLEFT:
|
// ret = 0
|
||||||
tv.setGravity(Gravity.LEFT);
|
case ALIGNLEFT:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ALIGNRIGHT:
|
case ALIGNRIGHT:
|
||||||
tv.setGravity(Gravity.RIGHT);
|
ret = w;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// default is align left ret = 0
|
||||||
default:
|
default:
|
||||||
tv.setGravity(Gravity.CENTER);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TextProperty{
|
private static class TextProperty{
|
||||||
int maxWidth;
|
int maxWidth;
|
||||||
int height;
|
int height;
|
||||||
|
int numberLines;
|
||||||
|
|
||||||
TextProperty(int w, int h){
|
TextProperty(int w, int h, int n){
|
||||||
this.maxWidth = w;
|
this.maxWidth = w;
|
||||||
this.height = h;
|
this.height = h;
|
||||||
|
this.numberLines = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TextProperty getTextWidthAndHeight(String content, int fontSize){
|
private static TextProperty getTextWidthAndHeight(String content, Paint paint){
|
||||||
Paint paint = new Paint();
|
|
||||||
paint.setColor(Color.WHITE);
|
|
||||||
paint.setTextSize(fontSize);
|
|
||||||
|
|
||||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
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");
|
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){
|
private static void initNativeObject(Bitmap bitmap){
|
||||||
|
|
Loading…
Reference in New Issue