Sunday, January 27, 2013

Drawing Layers and Color shapes on Bitmap


Here We can Draw the Colors and keep the multiple drawable images as Layers on Bitmab.
Thwe code is as follows.

Resources r = getResources();

        Drawable[] layers = new Drawable[3];

Drawing color on a bitmap as first layer
        layers[0] = writeOnDrawable((R.drawable.some, text);

Drawing bitmap from resources using R.drawable.some  as Second layer
layers[2] = r.getDrawable(ImgUtil.getImageId(R.drawable.some));

Drawing Border to the Bitmap  as Third layer
layers[1] = getBorder(ImgUtil.getImageId((R.drawable.some), "");


All the above layers as single image by using LayerDrawables class
        LayerDrawable layerDrawable = new LayerDrawable(layers);




public BitmapDrawable writeOnDrawable(int drawableId, String text, String combineTblId){

       Bitmap bm = BitmapFactory.decodeResource(getResources(),                     drawableId).copy(Bitmap.Config.ARGB_8888, true);
       Paint paint = new Paint();
//        paint.setStyle(Style.FILL_AND_STROKE);
       paint.setColor(Color.BLACK);
       paint.setTextSize(20);
       paint.setTypeface(Typeface.DEFAULT_BOLD);
     
       Canvas canvas = new Canvas(bm);
       canvas.drawText(text, bm.getWidth()/2-10, bm.getHeight()/2+8, paint);
       String color = combinedTableMap.get(combineTblId);
       if(color!= null){
       
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
       paint.setStrokeWidth(2);
       paint.setColor(Integer.parseInt(color));    
       paint.setStyle(Paint.Style.FILL_AND_STROKE);
       paint.setAntiAlias(true);
     
        Path path = new Path();
       path.setFillType(Path.FillType.EVEN_ODD);
       path.moveTo(0,0);
       path.lineTo(0,15);
       path.lineTo(15,0);
       path.lineTo(0,0);
       path.close();
       canvas.drawPath(path, paint);
       }
     
     
       /*paint.setStyle(Style.STROKE);
       paint.setColor(Color.RED);
       paint.setStrokeWidth(2);
       canvas.drawRect(new RectF(0,0, bm.getWidth(), bm.getHeight()), paint);*/
       return new BitmapDrawable(getActivity().getResources(), bm);
}




public BitmapDrawable getBorder(int drawable ,String color){

Bitmap bMap = BitmapFactory.decodeResource(getResources(), drawable);

   final int BORDER_WIDTH = 1;
   final int BORDER_COLOR = Color.RED;
   Bitmap res = Bitmap.createBitmap(bMap.getWidth() + 2 * BORDER_WIDTH,
                                    bMap.getHeight() + 2 * BORDER_WIDTH,
                                    bMap.getConfig());
   Canvas c = new Canvas(res);
   Paint p = new Paint();
   p.setColor(BORDER_COLOR);
   c.drawRect(0, 0, res.getWidth(), res.getHeight(), p);
   p = new Paint(Paint.FILTER_BITMAP_FLAG);
   c.drawBitmap(bMap, BORDER_WIDTH, BORDER_WIDTH, p);

   Matrix mat = new Matrix();
   mat.postRotate(350);
   Bitmap bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), mat, true);
   return new BitmapDrawable(getActivity().getResources(), bMapRotate);
}

No comments:

Post a Comment