先上一张效果图
这里花鸟字每一个字都是一张图片,实现过程即通过输入的文字去获取对应的图片,然后平铺绘制到一张图片上,格式可自己设置三五个一行都可以,把输入的文字合成到一张图片上,之后把图片居中显示出来就是界面上的效果
以下附上使用C++的一种实现方式,理论上只要所用语言支持图片绘制就可以移植或者自行实现
// 绘制代码
void CArt::Paint(Graphics& v_graphics)
{
v_graphics.TranslateTransform(m_nFrameWidth / 2.0f, m_nFrameHeight / 2.0f);
if (m_pBgImage && m_vetFileList.size() < 4) v_graphics.DrawImage(m_pBgImage, Rect(-m_nBgWidth / 2.0f, -m_nBgHeight / 2.0f, m_nBgWidth + 0.0f, m_nBgHeight + 0.0f), 0, 0, m_nBgWidth, m_nBgHeight, UnitPixel);
if (m_pFontImage) v_graphics.DrawImage(m_pFontImage, Rect(-m_nFontWidth / 2.0f, -m_nFontHeight / 2.0f, m_nFontWidth + 0.0f, m_nFontHeight + 0.0f), 0, 0, m_nFontWidth, m_nFontHeight, UnitPixel);
}
// 生成花鸟字图片
bool CArt::LoadImage(string strURI)
{
bool bRet = false;
int length = strURI.length();
if (length && length % 6 == 0) {
m_vetFileList.clear();
SAFE_DELETE(m_pFontImage);
for (int i = 0; i < length; i += 6) {
string str = strURI.substr(i, 6);
str += ".jpg";
m_vetFileList.emplace_back(str);
}
int size = m_vetFileList.size();
if (size > 0) {
m_nFontWidth = FONT_WIDTH * size;
m_nFontHeight = FONT_HEIGHT;
m_pFontImage = new Bitmap(m_nFontWidth, m_nFontHeight);
Graphics gs(m_pFontImage);
for (int i = 0; i < size; i++) {
string strFileName = g_strWorkDir + "/art/data/" + m_vetFileList[i];
CString strImage(strFileName.c_str());
Bitmap image(strImage);
gs.DrawImage(&image, Rect(i * FONT_WIDTH, 0, FONT_WIDTH, FONT_HEIGHT), 0, 0, FONT_WIDTH, FONT_HEIGHT, UnitPixel);
}
Update();
bRet = true;
}
}
return bRet;
}
// 按钮点击事件
void OnBtnAdd() {
CDuiString strText = m_pEditText->GetText();
string strUtf8;
bool bRet = tools::UnicodeToUtf8(strText.GetData(), strUtf8);
if (bRet) {
string strURI = encodeURIComponent(strUtf8, false);
CArt* pArt = new CArt();
if (pArt) {
bool bRet = pArt->LoadImage(strURI);
}
}
}
代码参考:https://download.csdn.net/download/dragonwarrior_95/20211368