2012-02-21 75 views

回答

0

程序:

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" /> 
+0

我在R.color和R.drawable中有一個顏色代碼....我想從它創建一個位圖...並在程序中使用它。我認爲你的代碼與 – Prasham 2012-02-21 06:49:28

+0

相反不行,我只是讓你填寫它。 – 2012-02-21 13:48:21

-1

//用法: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; 
    } 
+0

我期待看到R.drawable或R.color或來自資源的東西....這就是我想要和提到的問題以及..... DISAPPOINTMENT :( – Prasham 2012-02-23 13:43:03

0
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; 
    }