在android中将画布转换为位图图像

发布于 2024-09-29 06:38:31 字数 72 浏览 5 评论 0原文

我正在尝试在画布上开发一个应用程序,我正在画布上绘制位图。绘制后,我尝试将其转换为位图图像。

谁能给我一个建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

童话里做英雄 2024-10-06 06:38:31

建议取决于您想要做什么。

如果您担心控件需要很长时间才能绘制,并且想要绘制位图,以便可以位图位图传输而不是通过画布重新绘制,那么您想要双重猜测平台 - 控件自动将其绘图缓存到临时位图,甚至可以使用 getDrawingCache()

如果你想使用画布绘制位图,通常的方法是:

  1. 创建一个位图使用 Bitmap.createBitmap()
  2. 创建一个画布实例,并使用 Canvas(Bitmap) 构造函数
  3. 绘制到画布
  4. 使用位图

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
别低头,皇冠会掉 2024-10-06 06:38:31

因此,您创建一个新的 Bitmap,例如:

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

width< /code> 和 height 与您的画布相同。

接下来,使用canvas.setBitmap(myBitmap),但不使用drawBitmap()。

调用 setBitmap 后,您在画布上绘制的所有内容实际上都是按照我所演示的示例代码在 myBitmap 上绘制的。

编辑

您不能直接创建位图,例如:

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

您必须使用:

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

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 );
比忠 2024-10-06 06:38:31

其他例子:

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; 
}

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; 
}
删除会话 2024-10-06 06:38:31

以下是将画布转换为位图并将其存储到图库或特定文件夹的步骤。

注意:请确保您已授予 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. 创建父布局的引用

    LinearLayout LinearLayout = (LinearLayout) findViewById(R.id. LinearLayout);
    
  2. 将其存储到图库中

    final String imagename = UUID.randomUUID().toString() + ".png";
    MediaStore.Images.Media.insertImage(getContentResolver(), LinearLayout .getDrawingCache(), imagename, "绘图");
    
  3. 转换为位图

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

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());
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文