开发者

how to merge Android canvas?

开发者 https://www.devze.com 2023-04-03 19:47 出处:网络
I was wondering, how do I merge/join Android canvases. In the code below I have cnv_left which contains a left part of a button. cnv_center contains the center part. And cnv_text contains text.

I was wondering, how do I merge/join Android canvases. In the code below I have cnv_left which contains a left part of a button. cnv_center contains the center part. And cnv_text contains text.

What I need is to merge them all in cnv_joined , so that

  • cnv_left would go first.
  • then cnv_center.
  • cnv_text would be in center of cnv_center.
  • and a flipped cnv_left would be the last.

Here's my code so far:

 public void drawButt()
 {

      float buttonScale = 1.0f;     /// general button scale ratio
      float buttonScaleCnt = 6.0f;  /// button's center part stretch ratio

      LinearLayout LinLay = (LinearLayout)findViewById(R.id.linearLayout1);
      ImageView iv1 = new ImageView(this);

      Bitmap bit_left = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
     开发者_如何学C Canvas cnv_left = new Canvas(bit_left);
      cnv_left.scale(buttonScale,buttonScale);
      SVG svg_left = SVGParser.getSVGFromResource(getResources(), R.raw.btleft);
  Picture picture_left = svg_left.getPicture();
  picture_left.draw(cnv_left);

  Bitmap bit_center = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
      Canvas cnv_center = new Canvas(bit_center);
      cnv_center.scale(buttonScaleCnt, buttonScale);
      SVG svg_center = SVGParser.getSVGFromResource(getResources(), R.raw.btcnt);
  Picture picture_cnt = svg_center.getPicture();
  picture_cnt.draw(cnv_center);

  Bitmap bit_text = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
      Canvas cnv_text = new Canvas(bit_text);
  cnv_text.scale(buttonScale, buttonScale);
  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.WHITE); paint.setTextSize(20);
  cnv_text.drawText("R", 2, 30, paint);


  Bitmap bit_joined = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
  Canvas cnv_joined = new Canvas(bit_joined);  
  /// somehow need to somehow join the above canvases into this cnv_joined...


      iv1.setImageBitmap(bit_joined);
      iv1.setPadding(5, 5, 5, 5);

      LinLay.addView(iv1); 

 }

Any ideas? Oh, and one more thing, when I create empty bitmaps for my canvas ( Bitmap.createBitmap(100, 100... ), does it matter what size I give them? If yes, where should I get the correct sizes for them?

Thanks!


Size of Bitmaps matter. If you do Picture.draw to canvas smaller than Picture, image will be cropped to size of bitmap.

Call SVG.getBounds to get bounds and put them in Bitmap constructor.

To join Bitmaps together you have to draw bit_left, bit_center and bit_text on cnv_joined using drawBitmap.

Better way will be to draw SVGs and text directly on cnv_joined.

0

精彩评论

暂无评论...
验证码 换一张
取 消