转 自: http://blog.csdn.net/ygc87/article/details/6801453
java代码中的处理,方法一:
- //加载xml布局文件
- LayoutInflater factory = LayoutInflater.from(context);
- View view = factory.inflate(R.layout.main, null);
- //获得布局文件中的TextView
- TextView city = (TextView) view.findViewById(R.id.city);
- //设置city的文本信息
- city.setText("xml中的textview");
- //启用绘图缓存
- view.setDrawingCacheEnabled(true);
- //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null
- view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),
- MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));
- //这个方法也非常重要,设置布局的尺寸和位置
- view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
- //获得绘图缓存中的Bitmap
- view.buildDrawingCache();
- Bitmap bitmap = view.getDrawingCache();
java代码中的处理,方法二:
- //加载xml布局文件
- LayoutInflater factory = LayoutInflater.from(context);
- View view = factory.inflate(R.layout.main, null);
- //获得布局文件中的TextView
- TextView city = (TextView) view.findViewById(R.id.city);
- //设置city的文本信息
- city.setText("xml中的textview");
- //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null
- view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),
- MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));
- //这个方法也非常重要,设置布局的尺寸和位置
- view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
- //生成bitmap
- Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
- Bitmap.Config.RGB_565);
- //利用bitmap生成画布
- Canvas canvas = new Canvas(bitmap);
- //把view中的内容绘制在画布上
- view.draw(canvas);