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

来源:互联网 发布:网络招商 编辑:程序博客网 时间:2024/05/01 15:09

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




原创粉丝点击