android中创建有自身特色的字符串

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值