下面列出了android.widget.LinearLayout#draw ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Bitmap getBitmapFromView(String title, int dotBg) {
LinearLayout llmarker = (LinearLayout) findViewById(R.id.ll_marker);
TextView markerImageView = (TextView) findViewById(R.id.tv_title);
markerImageView.setText(title);
View dot = (View) findViewById(R.id.dot_marker);
dot.setBackgroundResource(dotBg);
llmarker.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(llmarker.getMeasuredWidth(), llmarker.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
llmarker.layout(0, 0, llmarker.getMeasuredWidth(), llmarker.getMeasuredHeight());
llmarker.draw(canvas);
return bitmap;
}
public Bitmap getBitmapFromView(String title, int dotBg) {
LinearLayout llmarker = (LinearLayout) findViewById(R.id.ll_marker);
TextView markerImageView = (TextView) findViewById(R.id.tv_title);
markerImageView.setText(title);
View dot = (View) findViewById(R.id.dot_marker);
dot.setBackgroundResource(dotBg);
llmarker.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(llmarker.getMeasuredWidth(), llmarker.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
llmarker.layout(0, 0, llmarker.getMeasuredWidth(), llmarker.getMeasuredHeight());
llmarker.draw(canvas);
return bitmap;
}
public void write(FormulaListView formulaListView, Bitmap.CompressFormat format) throws Exception
{
final LinearLayout f = formulaListView.getList();
Bitmap bitmap = null;
try
{
bitmap = Bitmap.createBitmap(f.getMeasuredWidth(), f.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
f.setBackgroundColor(CompatUtils.getThemeColorAttr(f.getContext(), android.R.attr.windowBackground));
f.draw(canvas);
}
catch (OutOfMemoryError e)
{
throw new Exception(e.getLocalizedMessage());
}
finally
{
f.setBackgroundResource(android.R.color.transparent);
}
bitmap.compress(format, 100, stream);
stream.flush();
}
/**
* 截取LinearLayout
**/
public static Bitmap getLinearLayoutBitmap(LinearLayout linearLayout) {
int h = 0;
Bitmap bitmap;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
h += linearLayout.getChildAt(i).getHeight();
}
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(linearLayout.getWidth(), h,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
linearLayout.draw(canvas);
return bitmap;
}