converting a canvas into bitmap image in android

一笑奈何 提交于 2019-11-26 01:59:30

问题


I am trying to develop an app on canvas,I am drawing a bitmap on the canvas.After drawing,i am trying to convert into bitmap image.

can anyone give me a suggestion.

thank you in advance.


回答1:


Advice depends upon what you are trying to do.

If you are concerned that your controls take a long time to draw, and you want to draw to a bitmap so you can blit the bitmap rather than re-drawing via a canvas, then you don't want to be double-guessing the platform - controls automatically cache their drawing to temporary bitmaps, and these can even be fetched from the control using getDrawingCache()

If you want to draw using a canvas to a bitmap, the usual recipe is:

  1. Create a bitmap of the correct size using Bitmap.createBitmap()
  2. Create a canvas instance pointing that this bitmap using Canvas(Bitmap) constructor
  3. Draw to the canvas
  4. Use the bitmap



回答2:


So you create a new Bitmap, for example:

Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 )

with width and height being the same as your canvas.

Next, use canvas.setBitmap(myBitmap), but not drawBitmap().

After you call setBitmap, all what you draw on canvas is in fact, drawing on your myBitmap going by the example code I have illustrated.

Edit:

You can not create a bitmap directly such as:

Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 );

You must use instead:

Bitmap myBitmap = Bitmap.createBitmap( (int)Width, (int)Height, Config.RGB_565 );



回答3:


Other example:

public Bitmap getBitmapNews(int item , boolean selected, int numbernews){                   
        Bitmap bitmap;

        if(selected)
            bitmap=mBitmapDown[item].copy(Config.ARGB_8888, true);
        else 
            bitmap=mBitmapUp[item].copy(Config.ARGB_8888, true);

        Canvas canvas = new Canvas(bitmap);

        if(numbernews<10){
        canvas.drawBitmap(mNotiNews[numbernews],0,0,null);
        }else{
            canvas.drawBitmap(mNotiNews[0],0,0,null);
        }

 return bitmap; 
}



回答4:


Following are the steps to convert from canvas to bitmap and storing it to gallery or specific folder.

Note: Make sure you have given permission of WRITE_EXTERNAL_STORAGE

activity_main.xml

            <LinearLayout
                android:id="@+id/linearLayout"
                android:orientation="horizontal"
                android:layout_margin="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <DrawingView
                    android:id="@+id/drawingView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

            </LinearLayout>

MainActivity.java

  1. Create reference of parent layout

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    
  2. To store it into gallery

    final String imagename = UUID.randomUUID().toString() + ".png";
    MediaStore.Images.Media.insertImage(getContentResolver(), linearLayout .getDrawingCache(), imagename, "drawing");
    
  3. To convert into bitmap

    linearLayout.setDrawingCacheEnabled(true);
    linearLayout.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(linearLayout.getDrawingCache());
    


来源:https://stackoverflow.com/questions/4013725/converting-a-canvas-into-bitmap-image-in-android

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