我試圖創建顏色代碼colors.xml安卓:試圖從顏色代碼位圖。它返回null
定義爲繪製位圖每當我試圖從它這條線創建位圖拋出一個空指針異常。
如何從特定顏色代碼創建位圖。無論哪種用作R.color
或R.drawable
我試圖創建顏色代碼colors.xml安卓:試圖從顏色代碼位圖。它返回null
定義爲繪製位圖每當我試圖從它這條線創建位圖拋出一個空指針異常。
如何從特定顏色代碼創建位圖。無論哪種用作R.color
或R.drawable
程序:
Bitmap result = Bitmap.createBitmap(48,48, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
// .....
canvas.drawBitmap(result, rect, rect, paint);
XML:
RES /抽拉:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bg_toolbar"
android:tileMode="repeat" />
//用法:genBitMapforAnim( 「5」,48)
protected Bitmap genBitMapforAnim(String itemCode, int size) {
String text = itemCode.length() >= 5 ? itemCode.substring(0, 5) : itemCode;
Bitmap result = Bitmap.createBitmap(size*2,size*2, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawARGB(0, 0xff, 0, 0);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setColor(Color.RED); //use your background color
paint.setAlpha(200);
canvas.drawCircle(size/2, size/2, size/2, paint);
paint.setAlpha(255);
paint.setTextSize(9);
paint.setColor(Color.WHITE); //use your text color
float textWidth = paint.measureText(text);
FontMetrics fm = paint.getFontMetrics();
float textHeight = (float)(fm.descent + fm.ascent)
canvas.drawText(text, (size-textWidth) /2.0f,(size-textHeight)/2.0f, paint);
final Rect rect = new Rect(0, 0,size, size);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(result, rect, rect, paint);
return result;
}
我期待看到R.drawable或R.color或來自資源的東西....這就是我想要和提到的問題以及..... DISAPPOINTMENT :( – Prasham 2012-02-23 13:43:03
protected Bitmap genBitMapforAnim(String itemCode, int size) {
String text = itemCode.length() >= 5 ? itemCode.substring(0, 5) : itemCode;
Bitmap result = Bitmap.createBitmap(size*2,size*2, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawARGB(0, 0xff, 0, 0);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setColor(context.getResources().getColor(R.color.id)); //use your background color
paint.setAlpha(200);
canvas.drawCircle(size/2, size/2, size/2, paint);
paint.setAlpha(255);
paint.setTextSize(9);
paint.setColor(Color.WHITE); //use your text color
float textWidth = paint.measureText(text);
FontMetrics fm = paint.getFontMetrics();
float textHeight = (float)(fm.descent + fm.ascent)
canvas.drawText(text, (size-textWidth) /2.0f,(size-textHeight)/2.0f, paint);
final Rect rect = new Rect(0, 0,size, size);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(result, rect, rect, paint);
return result;
}
我在R.color和R.drawable中有一個顏色代碼....我想從它創建一個位圖...並在程序中使用它。我認爲你的代碼與 – Prasham 2012-02-21 06:49:28
相反不行,我只是讓你填寫它。 – 2012-02-21 13:48:21