android提供的字符串的字体格式很受限制。这里分享一种创建有自身特色字符串的方法,以供大家参考。代码如下:

public static Bitmap getNumberBitmap(Resources res, String inputStr, float hRatio) {

final int imageWidth = 8;
final int imageHeight = 9;
char[] inputChar = inputStr.toCharArray();
Bitmap newBitmap = Bitmap.createBitmap(imageWidth * inputChar.length, imageHeight, Config.ARGB_8888);
Canvas cv = new Canvas(newBitmap);
Bitmap sourceBitmap = BitmapFactory.decodeResource(res, R.drawable.img_number);
Bitmap bitmap = null;
for (int i = 0; i < inputChar.length; i++) {
char c = inputChar[i];
switch (c) {
case '.':
bitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, imageWidth, imageHeight);
break;
case '/':
bitmap = Bitmap.createBitmap(sourceBitmap, 8, 0, imageWidth, imageHeight);
break;
case '0':
bitmap = Bitmap.createBitmap(sourceBitmap, 16, 0, imageWidth, imageHeight);
break;
case '1':
bitmap = Bitmap.createBitmap(sourceBitmap, 24, 0, imageWidth, imageHeight);
break;
case '2':
bitmap = Bitmap.createBitmap(sourceBitmap, 32, 0, imageWidth, imageHeight);
break;
case '3':
bitmap = Bitmap.createBitmap(sourceBitmap, 40, 0, imageWidth, imageHeight);
break;
case '4':
bitmap = Bitmap.createBitmap(sourceBitmap, 48, 0, imageWidth, imageHeight);
break;
case '5':
bitmap = Bitmap.createBitmap(sourceBitmap, 56, 0, imageWidth, imageHeight);
break;
case '6':
bitmap = Bitmap.createBitmap(sourceBitmap, 64, 0, imageWidth, imageHeight);
break;
case '7':
bitmap = Bitmap.createBitmap(sourceBitmap, 72, 0, imageWidth, imageHeight);
break;
case '8':
bitmap = Bitmap.createBitmap(sourceBitmap, 80, 0, imageWidth, imageHeight);
break;
case '9':
bitmap = Bitmap.createBitmap(sourceBitmap, 88, 0, imageWidth, imageHeight);
break;
}
cv.drawBitmap(bitmap, imageWidth * i, 0, null);
bitmap.recycle();
}

//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );
//store
cv.restore();

Bitmap expandBitmap = Bitmap.createScaledBitmap(newBitmap, Math.round(newBitmap.getWidth() * hRatio), Math.round(newBitmap.getHeight() * hRatio),true);
newBitmap.recycle();
return expandBitmap;

}


所使用的资源文件如下:

R.drawable.img_number




更多相关文章

  1. 分支和循环(二)(零基础学习C语言)
  2. android 设置状态栏颜色和字体颜色
  3. android中TextView分段显示不同颜色,字体,时间
  4. Android(安卓)将每三个数字加上逗号处理(通常使用金额方面的编辑)
  5. Android(安卓)汉字的正则表达式
  6. android中各种数据类型之间转换
  7. android studio错误: 常量字符串过长
  8. 【Android(安卓)开发教程】保存到内部存储设备
  9. My Android成长之路(二)——【JSON】

随机推荐

  1. 我的Android相关文章目录
  2. Java事件模型与Android事件模型的比较
  3. Service与Android系统设计(3)
  4. Android(安卓)P解决Socket通信Tcp粘包问
  5. android代码实现背景切换
  6. Android 与 Unity 交互一
  7. Android的第一个入门简单例子
  8. Android 4.4 KitKat升级率已经接近18%(20
  9. Android内核源码交叉编译
  10. 高焕堂android中文书全,电子文件for vers