Android:尝试从颜色代码中获取位图..它返回null

时间:2012-02-21 05:53:20

标签: android bitmap

我正在尝试使用colors.xml

中定义为drawable的颜色代码创建位图

每当我尝试从中创建一个位图时,该行抛出一个空指针异常。

如何根据特定颜色代码创建位图。用作R.colorR.drawable

3 个答案:

答案 0 :(得分: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" />  

答案 1 :(得分: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;  
    }  

答案 2 :(得分:-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;  
    }