6

我有一个视图是从布局膨胀的:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tagView = inflater.inflate(R.layout.activity_main, null);
TextView name = (TextView) tagView.findViewById(R.id.textView1);
name.setText("hello");

现在我想将膨胀的视图转换为位图

我应该怎么做?

4

1 回答 1

8

您可以按如下方式完成:

//first, View preparation
LayoutInflater inflater =
   (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tagView = inflater.inflate(R.layout.activity_main, null);
TextView name = (TextView) tagView.findViewById(R.id.textView1);
name.setText("hello");


//second, set the width and height of inflated view
tagView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
tagView.layout(0, 0, tagView.getMeasuredWidth(), tagView.getMeasuredHeight()); 


//third, finally conversion
final Bitmap bitmap = Bitmap.createBitmap(tagView.getMeasuredWidth(),
tagView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tagView.draw(canvas);

最后,你得到bitmap了你的膨胀tagView

于 2012-09-13T09:30:10.767 回答