在Android上LiveWallpaper使用布局资源 [英] Using Layout resources in a LiveWallpaper on Android

查看:277
本文介绍了在Android上LiveWallpaper使用布局资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您创建的Andr​​oid一个LiveWallpaper 2.2+你得到一个帆布(或任何3D当量)借鉴。我想用绘制一些元素的内置Android的UI工具,而不是使用画布命令构建一切从头开始或加载pre-渲染UI的位图。

When you create a LiveWallpaper in Android 2.2+ you get a canvas (or whatever the 3D equivalent is) to draw on. I'd like to draw some elements using the built-in Android UI tools rather than building everything from scratch using canvas commands or a loading a pre-rendered UI bitmap.

转换单一视图为位图工作正常。即这个伟大的工程:

Converting a single View to a Bitmap works fine. i.e. this works great:

// For example this works:
TextView view = new TextView(ctx);
view.layout(0, 0, 200, 100);
view.setText("test");
Bitmap b = Bitmap.createBitmap( 200, 100, Bitmap.Config.ARGB_8888);                
Canvas tempC = new Canvas(b);
view.draw(tempC);
c.drawBitmap(b, 200, 100, mPaint);

不过,转换的LinearLayout带着孩子会产生问题。您的只有的得到的LinearLayout本身并没有它的孩子。举例来说,如果我设置的LinearLayout有一个白色的背景,我收到了很好的呈现白色的盒子,但没有一个TextView的孩子都是在位图。我也试着使用DrawingCache有类似的结果。

But, converting a LinearLayout with children causes problems. You only get the LinearLayout itself and none of it's children. For example, if I set the LinearLayout to have a white background I get a nicely rendered white box, but none of the TextView children are in the Bitmap. I've also tried using DrawingCache with similar results.

在code我使用的是唯一的变化是一个额外的绘图命令立方体的例子。在LinearLayout中工作正常,举杯或以常规视图(即一切都很好地显示出来),在LiveWallpaper我得到的是LinearLayout中的背景上呈现。

The code I'm using is the cube example with the only changes being an extra draw command. The LinearLayout works fine as a toast or as a regular view (i.e. everything nicely shows up), on the LiveWallpaper all I get is the LinearLayout's background rendered.

inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout) inflater.inflate(com.example.android.livecubes.R.layout.testLinearLayout, null);
layout.layout(0, 0, 400, 200);

Bitmap b = Bitmap.createBitmap( 400, 200, Bitmap.Config.ARGB_8888);
Canvas tempC = new Canvas(b);
layout.draw(tempC);
c.drawBitmap(b, 10, 200, mPaint);

有谁知道,如果你需要做什么特别的事情来获得正确呈现给我的位图中的孩子?也就是说,我需要以某种方式做一些特别的东西,使布局使孩子们的休息吗?我应该写一个函数来递归地做一些事来所有的孩子?

Does anyone know if you need to do anything special to get the children rendered properly to my bitmap? i.e. do I need to somehow do something special to make the layout render the rest of the children? Should I write a function to recursively do something to all the children?

我能复合都要亲力亲为,但由于显示器是相当静态的(即我画这一次,并保持该位图的副本上绘制背景),这似乎对我更容易,还是pretty的效率。

I could composite everything myself but, since the display is fairly static (i.e. I draw this once and keep a copy of the bitmap to draw on the background) this seems easier on me and still pretty efficient.

编辑: 虽然挖多一点到布局的状态,它看起来好像布局没有进展下来的视图树(即LinearLayout中获取其布局计算当我打电话布局(),但孩子们有一个空(为0x0)的大小)。按照罗曼盖伊的职位在2008年<一href="http://groups.google.com/group/android-developers/browse_thread/thread/8318671d0313024/e32c50aff962e0a2"相对=nofollow> Android开发者帖子。你必须等待布局传递或强制布局自己。问题是我怎么能等待的布局传递从墙纸引擎的LinearLayout中没有连接到根视图组?而且我怎么能手动布置每个子元素,如果布局需要您设置的左,上,右,下的时候我不知道是什么,这些应该是。

While digging a bit more into the state of the Layout it looks as though the layout is not progressing down the view tree (i.e. the LinearLayout gets its layout computed when I call layout() but the children have a null (0x0) size). According to the Romain Guy's post in 2008 android developer post. You have to wait for the layout pass or force the layout yourself. The problem is how can I wait for a layout pass from a wall paper engine for a LinearLayout that is not attached to the root view group? And how can I manually layout each child element if layout requires you to set the left, top, right, bottom when I don't know what these should be.

我已经打过电话forceLayout的孩子,但它似乎并没有帮助的。我不知道如何布局框架在后台工作(除了它确实一个双向布局)。有没有办法手动让它做的布局传递,也就是现在呢?因为它不是一个活动,我不认为有很多的正常背景的东西正在发生的事情很我喜欢的方式。

I've tried calling forceLayout on the children but it doesn't seem to help either. I'm not sure how the layout framework works behind the scenes (besides that it does a two pass layout). Is there a way to manually make it do the layout pass, i.e. right now? Since it's not an Activity I don't think a lot of the normal background stuff is happening quite the way I'd like.

推荐答案

动态壁纸非常特别设计,不使用标准的UI部件。但是,可以使用它们无论如何。你将不得不强制布局传递自己先调用措施()上的查看,然后布局()。你可以在我的<一个更多信息href="https://docs.google.com/leaf?id=0B_VKZCqEnHblNDdiY2UxODgtYWNhNS00MmU4LWE4NDMtZjQ1OWI5MDMxZTVh&sort=name&layout=list&num=50">$p$psentation.

Live Wallpapers were very specifically designed to NOT use standard UI widgets. However it is possible to use them anyway. You will have to force a layout pass yourself by first calling measure() on the View, then layout(). You can find more information in my presentation.

这篇关于在Android上LiveWallpaper使用布局资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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