简体   繁体   English

我可以将XML布局转换为可绘制的吗?

[英]Can I turn an XML-layout into a drawable?

So I want to put an xml-layout as a background to a view, but then it has to be a drawable. 所以我想把一个xml-layout作为背景放到视图中,但是它必须是一个可绘制的。 So is there any way to turn an it into a drawable? 那么有没有办法把它变成一个可绘制的?

I've also tried inflating it, but nothing works the way I want :( 我也尝试过膨胀它,但没有任何方法可以按我想要的方式运行:(

Thx in advance. Thx提前。

Basically like @CommonsWare said. 基本上就像@CommonsWare所说的那样。
1. Get the LayoutInflator of the activity 1.获取活动的LayoutInflator
2. inflate the layout like inflater.inflate(R.layout.sample,null,false) 2.像inflater.inflate(R.layout.sample,null,false)一样给布局inflater.inflate(R.layout.sample,null,false)
3. Create a canvas, that saves all the drawings into a defined bitmap like this 3.创建一个画布,将所有图形保存到这样定义的位图中

Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
  1. Draw the created view into the canvas like this 像这样将创建的视图绘制到画布中

     View v = inflater.inflate(R.layout.sample,null,false); // inflate view here v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); v.draw(c); 
  2. Use the bitmap, the canvas did draw 使用位图,画布确实绘制

  3. Consider saving the bitmap locally in terms of redrawing of the activity, see following explanation: 考虑在重新绘制活动方面在本地保存位图,请参阅以下说明:

Depending on how dynamically and often you want to create this background via canvas you should furthermore consider to save the created bitmap somewhere (temporarily), because each time the layout changes (eg checkbox selection), the whole canvas has to be redrawn and the inflating process needs to be done a second time too, what can lead to runtime lags in the UI. 根据您想要通过画布动态创建此背景的方式,您还应考虑将创建的位图保存在某处(暂时),因为每次布局更改(例如复选框选择)时,必须重绘整个画布并进行膨胀流程也需要第二次完成,这会导致UI中的运行时滞后。 So just save this bitmap somewhere and reload it maybe, that's what i did in am similar case. 所以只需将这个位图保存在某处并重新加载它,这就是我在类似情况下所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM