从自定义SurfaceView获取位图 [英] Getting Bitmap from Custom SurfaceView

查看:454
本文介绍了从自定义SurfaceView获取位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在扩展表面视图并实现可运行的类中拥有此代码 我能够使用该类基本上允许您使用不同的颜色等绘制到画布上.我正在尝试一种方法,可以让我在绘制图像后保存图像,这就是该方法.不管我做什么,我都会得到一张黑色的图像,上面没有任何东西.有什么想法吗?

I have this code in a class that extends surface view and implements runnable I am able to use the class basically allows you to draw to the canvas using different colors and such. I'm trying to get a method that will allow me to save the image after it is drawn and this is the method. No matter what i do i just get a black image with nothing on it. Any ideas?

我已启用缓存绘图

客观地从自定义SurfaceView中获取位图图像,我已经用尽了查看此处其他文章的各种选择,但没有找到任何可行的方法.希望最近有一个新的解决方案.非常感谢

Objective get a bitmap image from a custom SurfaceView I have exhausted options of looking at some other post on here and didn't find anything to work. Here is hoping that there is recently a new solution to this. Many thanks

public Bitmap getImage() {
            Bitmap bitmap = Bitmap.createBitmap(this.getWidth(),
                    this.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            this.draw(canvas);
            return bitmap;
        }

推荐答案

只有通过您的最后评论,您的问题才变得清晰.在上面发布的代码中,您将返回return bitmap.这将返回局部变量bitmap.此局部变量完全空白.您可能会在代码中的其他位置绘制到位图或将图像与其关联.但是您上面的代码中的位图的实例是空白的,并且仅在函数本地.您不能期望它返回更新的最新位图.

Your question became clear only by your last comment. In the code that you posted above, you are returning with return bitmap. That will return the local variable bitmap. This local variable is totally blank. You may be drawing to your bitmap or associating an image to it, somewhere else in the code. But the instance of bitmap in your above code, is blank and only local to the function. You cannot expect it to return your updated, latest bitmap.

现在,在您发表评论后,我在Google上搜索了获取当前Surfaceview的位图",这使我想到了这样的答案:

Now, after your comment I googled "getting a bitmap of current surfaceview" and it led me to this SO answer: Create Bitmap from SurfaceView

在该问题中,显然可以通过扩展View而不是SurfaceView来解决.绘图缓存仅适用于View.

In that question, it was apparently solved by extending View instead of SurfaceView. Drawing cache only works for View.

更新:请遵循以下教程.根据您粘贴的代码,尚不清楚错误是什么.要绘制到SurfaceView,需要做很多事情,但我不确定您是否完成了这些工作,因此我无法要求所有此类缺失的物品.我遵循以下基本图形项目的教程.您需要阅读它们,看看您是否错过了任何事情.

Update: Follow the below tutorials. Based on the code pasted by you, it's not clear what the error is. There are a lot of things that need be done for drawing to SurfaceView, and I'm not sure if you have done those, and I cannot ask for every such missing item. I followed below tutorials for my basic graphics projects. You need to read them and see if you have missed anything.

画布绘图教程:

在Android中玩图形所有部分.

Android 2D

这篇关于从自定义SurfaceView获取位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆