首页 \ 问答 \ Android屏幕外视图仅绘制其背景而不是其他内容(Android offscreen view draws only its background and nothing else)

Android屏幕外视图仅绘制其背景而不是其他内容(Android offscreen view draws only its background and nothing else)

我正在尝试在屏幕外完全创建和渲染视图。 首先,我尝试过(来自http://arpitonline.com/2012/07/17/capturing-bitmaps-of-views-in-android/ ):

Bitmap result = Bitmap.createBitmap(1080, 1080, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
view.layout(0, 0, 1080, 1080);
view.draw(c);
return result;

它只渲染了我视图的背景颜色。 这是正确的大小和正确的背景颜色,但内容是完全空的。 然后我偶然发现了这个:

view.layout(0, 0, 1080, 1080);
view.invalidate();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Bitmap result = bmp.copy(Bitmap.Config.ARGB_8888, false);
view.destroyDrawingCache();
return result;

但同样,它会产生相同的结果:正确的大小和背景颜色与空内容。 我的视图中有很多视图,我在它们上设置了很多属性,没有空指针等,一切都设置正确。 但它们并没有被绘制出来。 为什么会这样?

更新:正如psking建议的那样,我已经将我的绘图逻辑移动到View#post但它仍然是相同的(虽然它需要更长的时间才能完成,这可能是视图实际做某事的线索)。 尝试了两种方法:

(完成是我的函数,采用位图)

final Bitmap result = Bitmap.createBitmap(1080, 1080, Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(result);
view.layout(0, 0, 1080, 1080);
view.post(new Runnable() {
    @Override
    public void run() {
        view.draw(c);
        completion.call(result);
    }
});

view.layout(0, 0, 1080, 1080);
view.invalidate();
view.setDrawingCacheEnabled(true);
view.post(new Runnable() {
    @Override
    public void run() {
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Bitmap result = bmp.copy(Bitmap.Config.ARGB_8888, false);
        view.destroyDrawingCache();
        completion.call(result);
    }
});

但还是一样。


I'm trying to create and render a view completely offscreen. First, I've tried (from http://arpitonline.com/2012/07/17/capturing-bitmaps-of-views-in-android/):

Bitmap result = Bitmap.createBitmap(1080, 1080, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
view.layout(0, 0, 1080, 1080);
view.draw(c);
return result;

It rendered only my view's background color. It's the correct size and correct background color, but the contents are completely empty. Then I've stumbled upon this:

view.layout(0, 0, 1080, 1080);
view.invalidate();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Bitmap result = bmp.copy(Bitmap.Config.ARGB_8888, false);
view.destroyDrawingCache();
return result;

But again, it yields the same result: correct size and background color with empty contents. There are many views inside my view, I set many properties on them, there are no null pointers etc, everything gets set correctly. But they are just not drawn. Why would this happen?

UPDATE: As psking suggested, I've moved my drawing logic to View#post but it's still the same (though it takes longer to fire the completion, which may be a clue to the view actually doing something). Tried both methods:

(completion is my function that takes a Bitmap)

final Bitmap result = Bitmap.createBitmap(1080, 1080, Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(result);
view.layout(0, 0, 1080, 1080);
view.post(new Runnable() {
    @Override
    public void run() {
        view.draw(c);
        completion.call(result);
    }
});

and

view.layout(0, 0, 1080, 1080);
view.invalidate();
view.setDrawingCacheEnabled(true);
view.post(new Runnable() {
    @Override
    public void run() {
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Bitmap result = bmp.copy(Bitmap.Config.ARGB_8888, false);
        view.destroyDrawingCache();
        completion.call(result);
    }
});

But still the same.


原文:https://stackoverflow.com/questions/32243249
更新时间:2019-06-22 04:58

最满意答案

int sizePixels = 1080;

Bitmap result = Bitmap.createBitmap(sizePixels, sizePixels, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);

// measure view first
int sizeSpec = View.MeasureSpec.makeMeasureSpec(sizePixels, View.MeasureSpec.EXACTLY);
view.measure(sizeSpec, sizeSpec);

// then layout
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
view.layout(0, 0, width, height);

// now you can draw it
view.draw(c);

int sizePixels = 1080;

Bitmap result = Bitmap.createBitmap(sizePixels, sizePixels, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);

// measure view first
int sizeSpec = View.MeasureSpec.makeMeasureSpec(sizePixels, View.MeasureSpec.EXACTLY);
view.measure(sizeSpec, sizeSpec);

// then layout
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
view.layout(0, 0, width, height);

// now you can draw it
view.draw(c);
2015-09-01

相关问答

更多

金属仅绘制到视图的一部分并查询图层(Metal only draws to a portion of the view and inquiry about layers)

您可以在init(coder)设置CAMetalLayer的框架,并且永不改变它。 但是在init(coder) ,最终的帧大小还不得而知(它只是使用您在此时在Interface Builder中设置的任何大小,而不是设备大小)。 在didMoveToWindow() ,还将Metal层的框架设置为其最终尺寸。 (或者也许在layoutSubviews() 。) You set the CAMetalLayer's frame in init(coder) and never change it. ...

RecyclerView在屏幕外绘图,无法将底部项目滚动到视图中(RecyclerView drawing offscreen, can't scroll bottom item into view)

我已经设法在v23上解决了这个问题,并为v22.2.1找到了解决方法。 这些行为之间的事实是完全不同的,这使我相信“按预期工作”并非全部真相。 v23修复:ViewPager上方的CustomDashboardLayout导致了问题,当它没有被强制使用visibility =“off”时,ViewPager根本没有被添加到层次结构中。 随着它的消失,ViewPager被添加,RecyclerView正确地调整其高度。 v22.2.1解决方法:v23修复对v22.2.1没有影响,解决方法是在Recy ...

如何在设置半透明属性时让UINavigationController不更改其视图大小?(How do I get a UINavigationController to NOT change its view size when setting the translucent property?)

对我viewDidLoad是在viewDidLoad添加extendedLayoutIncludesOpaqueBars = true 像这样的东西 override func viewDidLoad() { super.viewDidLoad() extendedLayoutIncludesOpaqueBars = true } 希望它也能为你工作 I found a solution that works, although it is indeed a bit of a hac ...

iOS 9导航栏平铺背景(iOS 9 Navigation Bar Tile Background)

以下是我对此的看法: 首先 ,iOS只提供两种状态栏外观 - 亮或默认。 重要的是要注意系统状态栏不再具有任何背景颜色。 当API引用UIStatusBarStyleLightContent ,它们表示清晰背景上的白色文本。 UIStatusBarStyleDefault是清晰背景上的黑色文本。 要在整个应用中将状态栏设置为白色: UIApplication.sharedApplication().statusBarStyle = .LightContent 要在整个应用中将状态栏设置为黑色: ...

Android视图拖放 - 只有在匹配特定视图位置时才接受视图丢弃?(Android view drag drop - accept view drop only if it matches certain view position?)

这已经很晚了! 但是我很多天前成功地完成了它,现在我有时间分享我的解决方案以防任何人感兴趣...我将解释我是如何设法在下面的简化示例上做到的,因为我的原始代码非常大连接 如上面我的说明性示例中所示,有:2个图像视图(let1,let2),带有两个相对布局(let1Container,let2Container)此外,还有两个空白图像视图(slot1,slot2),用作放置区域 ,以及他们的容器(slot1Container,slot2Container)类型(Relative Layout)全部放 ...

将背景图像设置为视图延伸了我的视图(Setting a background image to a view stretches my view)

据我所知,你面临的问题不是问题,它是如何使用Android设计布局的方式。 这意味着您可以使用3个默认常量值设置高度和宽度: FILL_PARENT View请求的高度或宽度的特殊值。 FILL_PARENT意味着视图想要与父视图一样大,如果有,则减去父视图的填充。 此值MATCH_PARENT API级别8开始弃用,并由MATCH_PARENT取代。 MATCH_PARENT View请求的高度或宽度的特殊值。 MATCH_PARENT意味着视图想要和父视图一样大,如果有的话减去父视图的填充。 ...

在HTML / CSS中,如何处理图像以使其不会脱离屏幕,需要滚动?(In HTML/CSS, how can images be treated such that they do not go offscreen, requiring scrolling?)

而不是使用img标签,请按如下所示设置div的背景。 background-size: contain; background-image: url(http://i.imgur.com/hPvyf52.png); height: 100%; width: 100% 然后交换背景图像。 Instead of using an img tag, set the background of the div as follows. background-size: contain; backgroun ...

如何仅绘制Xamarin.Forms按钮的背景?(How to draw only the background of a Xamarin.Forms button?)

我如何检测OnElementPropertyChanged中的FontSize值的更改 当你改变你的Xamarin.Forms.Button FontSize属性时,你可以像这样在OnElementPropertyChanged方法中检测到这种改变: protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { base. ...

相关文章

更多

最新问答

更多
  • Android宽度:100%修复(网站接管问题)(Android width:100% fix (website takeover issue))
  • C ++函数/方法设计的良好实践(Good practice in C++ function/method design)
  • 计算其他表中不存在的所有记录 - SQL查询(Count all records that does not exist to other table - SQL Query)
  • 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)
  • asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)
  • 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)
  • ng-class搞乱了类的顺序(ng-class messing with the order of classes)
  • oracle 12g无效数字错误(oracle 12g invalid number error)
  • 更改ng-src值onclick(Change ng-src value onclick)
  • 如何在android中自动添加自定义依赖项以创建新项目?(How to add custom dependencies automatically in android for ever a new project is created?)
  • datetime函数在PHP中(datetime function in php)
  • 在javascript中获取会话数组的值(in javascript get the value of a session array)
  • 如何在UTF8中编译LaTeX?(How can I compile LaTeX in UTF8? [closed])
  • Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)
  • Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)
  • JavaFX的。(JavaFX. Adding items to the list in different threads is not working)
  • 从GDATA日历资源迁移到Google Calendar Resource api(Migrate from GDATA calendar resource to Google Calendar Resource api)
  • SSRS 2008 - 以零情景处理分割(SSRS 2008 - Dealing with division by zero scenarios)
  • 我如何以编程方式添加一个listView列标题的点击事件(How can I add a listView column header a click event programmatically)
  • Wxpython:无法检索有关列表控件项XXX的信息(Wxpython: Couldn't retrieve information about list control item XXX)
  • 使用Tortoise SVN在SVN存储库中移动目录(Move Directory across SVN repository using Tortoise SVN)
  • 天蓝色服务结构集群中的web api无状态服务是否在一段时间不活动后进入休眠状态?(Do web api stateless services in azure service fabric cluster go to sleep after a period of inactivity?)
  • 我可以设置intelliJ来突出显示PHP编码风格吗?(Can I set intelliJ to highlight php coding style?)
  • 用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)
  • Spring ftp配置错误(Spring ftp configuration is wrong)
  • 使用gsub去除多个字符(Using gsub to strip multiple characters)
  • 续订推送证书并保持当前的App Store App正常工作(Renew Push certificate and keep current App Store App working)
  • js:ES5和ES6之间关于'this'关键字用法的一个令人困惑的观点(js: one confusing point about 'this' keyword usage between ES5 and ES6)
  • window.onload vs $(document).ready()(window.onload vs $(document).ready())
  • 在Swift中,如何声明一个符合一个或多个协议的特定类型的变量?(In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?)