/****************************************************************************
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.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.LinkedList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.FloatMath;
import android.util.Log;
public class Cocos2dxBitmap {
// ===========================================================
// Constants
// ===========================================================
/* The values are the same as cocos2dx/platform/CCImage.h. */
private static final int HORIZONTALALIGN_LEFT = 1;
private static final int HORIZONTALALIGN_RIGHT = 2;
private static final int HORIZONTALALIGN_CENTER = 3;
private static final int VERTICALALIGN_TOP = 1;
private static final int VERTICALALIGN_BOTTOM = 2;
private static final int VERTICALALIGN_CENTER = 3;
// ===========================================================
// Fields
// ===========================================================
private static Context sContext;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
public static void setContext(final Context pContext) {
Cocos2dxBitmap.sContext = pContext;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
private static native void nativeInitBitmapDC(final int pWidth, final int pHeight, final byte[] pPixels);
/**
* @param pWidth
* the width to draw, it can be 0
* @param pHeight
* the height to draw, it can be 0
*/
public static void createTextBitmap(String pString, final String pFontName, final int pFontSize, final int pAlignment, final int pWidth, final int pHeight) {
final int horizontalAlignment = pAlignment & 0x0F;
final int verticalAlignment = (pAlignment >> 4) & 0x0F;
pString = Cocos2dxBitmap.refactorString(pString);
final Paint paint = Cocos2dxBitmap.newPaint(pFontName, pFontSize, horizontalAlignment);
final TextProperty textProperty = Cocos2dxBitmap.computeTextProperty(pString, pWidth, pHeight, paint);
final int bitmapTotalHeight = (pHeight == 0 ? textProperty.mTotalHeight : pHeight);
final Bitmap bitmap = Bitmap.createBitmap(textProperty.mMaxWidth, bitmapTotalHeight, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
/* Draw string. */
final FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
int x = 0;
int y = Cocos2dxBitmap.computeY(fontMetricsInt, pHeight, textProperty.mTotalHeight, verticalAlignment);
final String[] lines = textProperty.mLines;
for (final String line : lines) {
x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment);
canvas.drawText(line, x, y, paint);
y += textProperty.mHeightPerLine;
}
Cocos2dxBitmap.initNativeObject(bitmap);
}
private static Paint newPaint(final String pFontName, final int pFontSize, final int pHorizontalAlignment) {
final Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(pFontSize);
paint.setAntiAlias(true);
/* Set type face for paint, now it support .ttf file. */
if (pFontName.endsWith(".ttf")) {
try {
final Typeface typeFace = Cocos2dxTypefaces.get(Cocos2dxBitmap.sContext, pFontName);
paint.setTypeface(typeFace);
} catch (final Exception e) {
Log.e("Cocos2dxBitmap", "error to create ttf type face: " + pFontName);
/* The file may not find, use system font. */
paint.setTypeface(Typeface.create(pFontName, Typeface.NORMAL));
}
} else {
paint.setTypeface(Typeface.create(pFontName, Typeface.NORMAL));
}
switch (pHorizontalAlignment) {
case HORIZONTALALIGN_CENTER:
paint.setTextAlign(Align.CENTER);
break;
case HORIZONTALALIGN_RIGHT:
paint.setTextAlign(Align.RIGHT);
break;
case HORIZONTALALIGN_LEFT:
default:
paint.setTextAlign(Align.LEFT);
break;
}
return paint;
}
private static TextProperty computeTextProperty(final String pString, final int pWidth, final int pHeight, final Paint pPaint) {
final FontMetricsInt fm = pPaint.getFontMetricsInt();
final int h = (int) Math.ceil(fm.bottom - fm.top);
int maxContentWidth = 0;
final String[] lines = Cocos2dxBitmap.splitString(pString, pWidth, pHeight, pPaint);
if (pWidth != 0) {
maxContentWidth = pWidth;
} else {
/* Compute the max width. */
int temp = 0;
for (final String line : lines) {
temp = (int) FloatMath.ceil(pPaint.measureText(line, 0, line.length()));
if (temp > maxContentWidth) {
maxContentWidth = temp;
}
}
}
return new TextProperty(maxContentWidth, h, lines);
}
private static int computeX(final String pText, final int pMaxWidth, final int pHorizontalAlignment) {
int ret = 0;
switch (pHorizontalAlignment) {
case HORIZONTALALIGN_CENTER:
ret = pMaxWidth / 2;
break;
case HORIZONTALALIGN_RIGHT:
ret = pMaxWidth;
break;
case HORIZONTALALIGN_LEFT:
default:
break;
}
return ret;
}
private static int computeY(final FontMetricsInt pFontMetricsInt, final int pConstrainHeight, final int pTotalHeight, final int pVerticalAlignment) {
int y = -pFontMetricsInt.top;
if (pConstrainHeight > pTotalHeight) {
switch (pVerticalAlignment) {
case VERTICALALIGN_TOP:
y = -pFontMetricsInt.top;
break;
case VERTICALALIGN_CENTER:
y = -pFontMetricsInt.top + (pConstrainHeight - pTotalHeight) / 2;
break;
case VERTICALALIGN_BOTTOM:
y = -pFontMetricsInt.top + (pConstrainHeight - pTotalHeight);
break;
default:
break;
}
}
return y;
}
/*
* If maxWidth or maxHeight is not 0, split the string to fix the maxWidth and maxHeight.
*/
private static String[] splitString(final String pString, final int pMaxWidth, final int pMaxHeight, final Paint pPaint) {
final String[] lines = pString.split("\\n");
String[] ret = null;
final FontMetricsInt fm = pPaint.getFontMetricsInt();
final int heightPerLine = (int) Math.ceil(fm.bottom - fm.top);
final int maxLines = pMaxHeight / heightPerLine;
if (pMaxWidth != 0) {
final LinkedLi
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Cocos2d-x 喵星战争 源代码 android版 (485个子文件)
libcocos2d.a 11.34MB
resources.ap_ 1.26MB
HelloCpp.apk 2.45MB
Cocos2dxBitmap.class 10KB
Cocos2dxSound.class 8KB
Cocos2dxGLSurfaceView.class 8KB
Cocos2dxHelper.class 7KB
Cocos2dxEditBoxDialog.class 6KB
Cocos2dxMusic.class 4KB
Cocos2dxRenderer.class 4KB
Cocos2dxActivity.class 4KB
Cocos2dxTextInputWraper.class 3KB
Cocos2dxAccelerometer.class 3KB
Cocos2dxHandler.class 3KB
Cocos2dxGLSurfaceView$1.class 2KB
Cocos2dxSound$OnLoadCompletedListener.class 2KB
Cocos2dxEditBoxDialog$2.class 1KB
Cocos2dxEditText.class 1KB
Cocos2dxTypefaces.class 1KB
Cocos2dxBitmap$TextProperty.class 1KB
Cocos2dxGLSurfaceView$4.class 1KB
Cocos2dxGLSurfaceView$7.class 1KB
Cocos2dxGLSurfaceView$9.class 1018B
Cocos2dxGLSurfaceView$5.class 1017B
Cocos2dxGLSurfaceView$6.class 1016B
Cocos2dxGLSurfaceView$8.class 1009B
Cocos2dxEditBoxDialog$1.class 963B
Cocos2dxGLSurfaceView$11.class 948B
Cocos2dxGLSurfaceView$10.class 926B
Cocos2dxHandler$1.class 864B
Cocos2dxGLSurfaceView$12.class 854B
Cocos2dxGLSurfaceView$2.class 840B
Cocos2dxGLSurfaceView$3.class 838B
Cocos2dxHandler$EditBoxMessage.class 787B
Cocos2dxSound$SoundInfoForLoadedCompleted.class 757B
Cocos2dxHelper$1.class 633B
HelloCpp.class 626B
Cocos2dxHandler$DialogMessage.class 585B
R.class 454B
Cocos2dxHelper$Cocos2dxHelperListener.class 430B
R$drawable.class 409B
R$string.class 407B
BuildConfig.class 351B
R$attr.class 349B
.classpath 459B
OpenSLEngine.cpp 18KB
SimpleAudioEngineJni.cpp 13KB
SimpleAudioEngine.cpp 8KB
GameView.cpp 6KB
GameMenu.cpp 5KB
GameObjHero.cpp 4KB
GameObjEnemy.cpp 3KB
GameAbout.cpp 3KB
SimpleAudioEngineOpenSL.cpp 3KB
GameMark.cpp 3KB
AppDelegate.cpp 1KB
main.cpp 1KB
GameEnemyBullet.cpp 1KB
GameHeroBullet.cpp 865B
main.o.d 40KB
GameMenu.o.d 40KB
GameView.o.d 39KB
AppDelegate.o.d 39KB
GameObjEnemy.o.d 39KB
GameObjHero.o.d 39KB
HelloWorldScene.o.d 39KB
testBase.o.d 39KB
Test1.o.d 39KB
GameAbout.o.d 39KB
GameEnemyBullet.o.d 39KB
GameHeroBullet.o.d 39KB
VisibleRect.o.d 39KB
GameMark.o.d 39KB
cocos2d.o.d 39KB
CCDirector.o.d 28KB
CCTransition.o.d 24KB
CCTextureCache.o.d 23KB
CCTransitionProgress.o.d 23KB
CCSprite.o.d 23KB
CCLayer.o.d 22KB
CCTransitionPageTurn.o.d 22KB
CCSpriteBatchNode.o.d 22KB
CCLabelBMFont.o.d 22KB
CCParticleBatchNode.o.d 22KB
CCMenu.o.d 22KB
CCDrawingPrimitives.o.d 22KB
CCActionTiledGrid.o.d 21KB
CCLabelAtlas.o.d 21KB
CCActionGrid3D.o.d 21KB
CCActionGrid.o.d 21KB
CCGrid.o.d 21KB
CCProgressTimer.o.d 21KB
Java_org_cocos2dx_lib_Cocos2dxRenderer.o.d 21KB
TouchesJni.o.d 21KB
CCEGLView.o.d 21KB
CCAction.o.d 21KB
CCAtlasNode.o.d 21KB
CCTextFieldTTF.o.d 20KB
CCTileMapAtlas.o.d 20KB
CCCamera.o.d 20KB
共 485 条
- 1
- 2
- 3
- 4
- 5
资源评论
- 测试NULL2014-03-19eclipse编译 肿么配置噢,不会~~
- u134982014-06-17好复杂的coco2dx,我的程度还不够
- huang1987hh2014-06-18怎么在eclipse中编译???
- goodsuper32013-10-30如果需要重新compile的話 會出滿多問題的 希望給點教學指南
- GYY_珠珠2013-10-11好复杂的coco2dx,我的程度还不够
qin3956
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 微信自动抢红包APP.zip
- 微信抢红包神器-红包鸟(可以抢多种情况下的红包,支持手机息屏抢红包、屏蔽群抢红包).zip
- 微信抢红包,支持xposed与免root,xposed支持最新版,免root需自己实现.zip
- 一维卷积神经网络英语电影评论情感分类项目功能实现-Embedding层
- 微信 自动抢红包 插件外挂(Android),实现了监控通知栏,自动点击红包等功能,帮助大家快速抢到红包.zip
- 满满的干货:分享二十个Python学习资源材料.zip
- USD ISO14229-1-2013 中文版
- STM32单片机FPGA毕设电路原理论文报告模糊控制器在无线监控机群系统中的应用
- OpenWrt-Toolchain-ipq
- STM32单片机FPGA毕设电路原理论文报告模糊控制逆变弧焊电源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功