How to draw Bitmap from ARGB_8888 Bitmap

白昼怎懂夜的黑 提交于 2019-12-25 06:35:46

问题


I am drawing paths on canvas and creating Transparent bitmap while saving and creating cropping bitmap from transparent bitmap.

See Images :

In this I mage I am drawing path on canvas and I am creating transparent bitmap and according to startX,lowestY and highestX,highestY

Bitmap cropBitmap =Bitmap.createBitmap(sourceBitmap,startX,lowestY,highestX,highestY);

When I am cropping Bitmap I want Only "Test" drawing crop bitmap.But it's giving empty bitmap. Like this

Inside red box I want cropped bitmap from transparent bitmap whatever I draw on canvas.


回答1:


You cannot simply create a transparent bitmap, and assume that whatever is within the bounds of the bitmap has become a part of it meaning that the pixels are identical. There are two problems with your assumption, number 1, a transparent bitmap won't help because transparency serves as a see through bitmap which only will have alpha value, number 2, the data that is drawn with the path, has no correlation to the data of your bitmap, you initially created a bitmap with no color, except that it's completely transparent.

Here's the general code for the correct way how to achieve such a task:

 class myDrawingView extends View() {
      // all your class members you initialize here
      @Override
      public void onDraw(Canvas canvas) {
          // get your width and height using startx, starty, highestx, highesty, lowesty
          Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
          canvas.setBitmap(buffer);
          canvas.drawPath(//draw your path here);
          invalidate();

      }

The Bitmap called buffer, now is the frame buffer for all your drawing so you can draw a path and that will be rendered on the buffer you defined. And you see that no transparency was used we just simply created a bitmap with no pixels assigned to it at first, once you draw to it via the canvas, your bitmaps pixels will be whatever you drew.



来源:https://stackoverflow.com/questions/24400602/how-to-draw-bitmap-from-argb-8888-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!