iOS 下使用第三方的 .ttf 字体文件需要在 info.plist 文件里面注册,
android 下面则不必如此麻烦,不用在其他 xxx.xml 配置文件中注册,
比如创建一个 CCLabelTTF,只要调用 create,在其中传入 ttf 文件名作为参数就可以了
TestFontLayer.h
#ifndef __testFont__TestFontLayer__
#define __testFont__TestFontLayer__
#include <stdio.h>
#include "cocos2d.h"
using namespace cocos2d;
#define SCENE_FUNC(__TYPE__) \
static CCScene* scene() \
{ \
CCScene* t_pScene = CCScene::create(); \
__TYPE__* t_pLayer = __TYPE__::create(); \
t_pScene->addChild(t_pLayer); \
return t_pScene; \
}
class TestFontLayer : public CCLayer {
public:
CREATE_FUNC(TestFontLayer);
SCENE_FUNC(TestFontLayer);
private:
virtual bool init();
};
#endif /* defined(__testFont__TestFontLayer__) */
TestFontLayer.cpp
#include "TestFontLayer.h"
bool TestFontLayer::init() {
if (!CCLayer::init()) {
return false;
}
CCSize t_oSize = CCDirector::sharedDirector()->getWinSize();
CCPoint t_oPtPosT = ccp(t_oSize.width * 0.5f, t_oSize.height * 0.75f);
CCPoint t_oPtPosM = ccp(t_oSize.width * 0.5f, t_oSize.height * 0.50f);
CCPoint t_oPtPosB = ccp(t_oSize.width * 0.5f, t_oSize.height * 0.25f);
{
// fonts/arial.ttf
CCLabelTTF* t_pLabel0 = CCLabelTTF::create("我是", "fonts/arial.ttf", 75);
t_pLabel0->setPosition(t_oPtPosT);
t_pLabel0->setAnchorPoint(ccp(0.5f, 0.f));
t_pLabel0->setColor(ccRED);
this->addChild(t_pLabel0);
CCLabelTTF* t_pLabel1 = CCLabelTTF::create("1aA#", "fonts/arial.ttf", 75);
t_pLabel1->setPosition(t_oPtPosT);
t_pLabel1->setAnchorPoint(ccp(0.5f, 1.f));
t_pLabel1->setColor(ccBLUE);
this->addChild(t_pLabel1);
}
{
// fonts/Marker Felt.ttf
CCLabelTTF* t_pLabel0 = CCLabelTTF::create("我是", "fonts/Marker Felt.ttf", 75);
t_pLabel0->setPosition(t_oPtPosM);
t_pLabel0->setAnchorPoint(ccp(0.5f, 0.f));
t_pLabel0->setColor(ccRED);
this->addChild(t_pLabel0);
CCLabelTTF* t_pLabel1 = CCLabelTTF::create("1aA#", "fonts/Marker Felt.ttf", 75);
t_pLabel1->setPosition(t_oPtPosM);
t_pLabel1->setAnchorPoint(ccp(0.5f, 1.f));
t_pLabel1->setColor(ccBLUE);
this->addChild(t_pLabel1);
}
{
// 下
CCLabelTTF* t_pLabel0 = CCLabelTTF::create("我是", "fonts/LiShu.ttf", 75);
t_pLabel0->setPosition(t_oPtPosB);
t_pLabel0->setAnchorPoint(ccp(0.5f, 0.f));
t_pLabel0->setColor(ccRED);
this->addChild(t_pLabel0);
CCLabelTTF* t_pLabel1 = CCLabelTTF::create("1aA#", "fonts/LiShu.ttf", 75);
t_pLabel1->setPosition(t_oPtPosB);
t_pLabel1->setAnchorPoint(ccp(0.5f, 1.f));
t_pLabel1->setColor(ccBLUE);
this->addChild(t_pLabel1);
}
return true;
}
工程的部分目录结构:
ProjectName/Classes/TestFontLayer.hProjectName/Classes/TestFontLayer.cpp
ProjectName/Resources/fonts/arial.ttf
ProjectName/Resources/fonts/Marker Felt.ttf
ProjectName/Resources/fonts/LiShu.ttf
运行效果:
2dx223 工程创建好后,proj.android 目录下就有这些东西
ant.properties
proguard-project.txt
project.properties
README.md
----------
res/drawable-hdpi/icon.png
res/drawable-ldpi/icon.png
res/drawable-mdpi/icon.png
res/values/strings.xml
----------
src/com/xxx/testFont/testFont.java
----------
jni/hellocpp/main.cpp
jni/Android.mk
jni/Application.mk
----------
AndroidManifest.xml
build_native.sh
build.xml
生成的 android 工程导入 adt 竟发现缺胳膊短腿儿?
只下载了 android-sdk-19,2dx223 生成的 android 工程中,
project.properties 的 target 值为 android-8,
导致 android-sdk 库 Android_4.4.2 都没有被引入到工程中
1,将 target 的值由 android-8 修改为 android-19 后,F5 刷新一下工程即发现 a4.4.2 库被引入了
2,将该工程 clean 一次,rebuild 后就会出现 gen 的源码文件夹
projName/gen/com.xxx.projname/BuildConfig.java
projName/gen/com.xxx.projname/R.java
以上两个文件就是会被生成出来的
3,至于 src/org/cocos2dx.lib,如果自动复制到生成的 android 工程中去,
可以去 cocos2d-x-2.2.3/cocos2dx/platform/android/java/src 中去拿
如何修改横竖屏?
AndroidManifest.xml 文件中
将 android:screenOrientation="landscape" 修改为
android:screenOrientation="portrait",横屏即会变成竖屏