如何将线性布局转换为图像
Posted
技术标签:
【中文标题】如何将线性布局转换为图像【英文标题】:How to convert linearlayout to image 【发布时间】:2012-05-20 15:04:46 【问题描述】:我想将完整的 layout
及其内容 (views
) 转换为可绘制图像?
【问题讨论】:
可能重复:***.com/questions/5776684/… 【参考方案1】:试试下面的代码:
public class androidWebImage extends Activity
ImageView bmImage;
LinearLayout view;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (LinearLayout)findViewById(R.id.screen);
bmImage = (ImageView)findViewById(R.id.image);
view.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
bmImage.setImageBitmap(b);
;
好好看看,你得用:
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
我使用了以下 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/screen"
android:layout_
android:layout_
>
<TextView
android:layout_
android:layout_
android:text="@string/hello"
/>
<ImageView
android:id="@+id/image"
android:layout_
android:layout_
/>
</LinearLayout>
结果是:
Reference
【讨论】:
你救了我哥们以上是关于如何将线性布局转换为图像的主要内容,如果未能解决你的问题,请参考以下文章