getDrawingCache()返回null [英] getDrawingCache() returns null

查看:235
本文介绍了getDrawingCache()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个简单的绘画应用程序,并试图实现给予更多的空间画出当用户请求,我认为可以用我的CustomView类的简单实现滚动(其中包含在LinearLayout中完成的功能然后在滚动型类)。如果我不通过运行块1调整CustomView类,块2工作正常(它只是节省绘制一个屏幕,在这个时候,有没有滚动。)一个坏消息!块2失败(view.getDrawingCache()返回null)时,块1运行(此时,滚动启用)。我想离开了拯救整个视图,包括部分原因是由于滚动。

块1:

  CustomView视图=(CustomView)findViewById(R.id.customViewId);
身高= 2 *高;
view.setLayoutParams(新LinearLayout.LayoutParams(宽度,高度));
 

块2:

  CustomView视图=(CustomView)findViewById(R.id.customViewId);
view.setDrawingCacheEnabled(真正的);
点阵位图= Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(假);
 

的$这两大块C $ CS有两种不同的方法,独立的和小的部分。

编辑: 后十亿谷歌的查询,这个问题现已解决) 我提到这个线程,的https://groups.google.com/forum/?fromgroups=#!topic/android-developers/VQM5WmxPilM. 我不明白的是getDrawingCache()方法对视图类的大小(宽度和高度)的限制。如果View类是太大,getDrawingCache()只是返回null。 因此,该解决方案是不使用的方法,而是做这样以下

  CustomView视图=(CustomView)findViewById(R.id.customViewId);
点阵位图= Bitmap.createBitmap(view.getMeasuredWidth()
  view.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
帆布bitmapHolder =新的Canvas(位);
view.draw(bitmapHolder);
//位图现在包含我们需要的数据!做任何与它!
 

解决方案

您需要调用 buildDrawingCache(),能够使用位图前。

setDrawingCache(真)的东西只是设置了标志,并以创建位图缓存等待下一个绘图通。

另外不要忘记调用 destroyDrawingCache()当你不再需要它。

I'm working on a simple painting app and trying to implement the functionality of giving more space to draw when a user requests, which I thought could be done by simply enabling scrolling of my CustomView class(which is contained in a LinearLayout then in a ScrollView class). If I didn't resize the CustomView class by running Chunk 1, Chunk 2 works fine(It simply saves one screen of drawing. At this time, there is no scrolling.) A bummer! Chunk 2 fails(view.getDrawingCache() returns null) when Chunk 1 is run (At this time, scrolling is enabled). I want to save the entire view including the part left out due to scrolling.

Chunk 1:

CustomView view = (CustomView) findViewById(R.id.customViewId);
height = 2 * height;
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));

Chunk 2:

CustomView view = (CustomView) findViewById(R.id.customViewId);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

These two chunks of codes are independent and small parts in two different methods.

Edited: After billions of Google queries, the problem is now SOLVED ;) I referred to this thread, https://groups.google.com/forum/?fromgroups=#!topic/android-developers/VQM5WmxPilM. What I didn't understand was getDrawingCache() method has the limit on the View class's size(width and height). If the View class is too big, getDrawingCache() simply returns null. So, the solution was not to use the method and instead do it like below.

CustomView view = (CustomView) findViewById(R.id.customViewId);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), 
  view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 
Canvas bitmapHolder = new Canvas(bitmap); 
view.draw(bitmapHolder);
// bitmap now contains the data we need! Do whatever with it!

解决方案

You need to call buildDrawingCache(), before being able to use the bitmap.

The setDrawingCache(true) thing just sets the flag and waits for the next drawing pass in order to create the cache bitmap.

Also don't forget to call destroyDrawingCache() when you no longer need it.

这篇关于getDrawingCache()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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