布局转成图片、android getDrawingCache D
2021-04-12 本文已影响0人
颤抖的闪电
//布局转成图片
public static Drawable LayoutToDrawable(Context context, View view) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); // 原始大小
// view.buildDrawingCache();
// Bitmap bitmap = view.getDrawingCache();
Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
view.draw(c);
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), b);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
}