问题
I am creating an image from a view in android and adding another image to the bottom of it with the code below
CardView card_view = (CardView) tmps.findViewById(R.id.card_view);
Bitmap b = Bitmap.createBitmap(card_view.getWidth(), card_view.getHeight() + 112, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
card_view.draw(c);
c.translate(card_view.getWidth() / 2 - 51, card_view.getHeight() + 10);
Drawable d = context.getResources().getDrawable(R.drawable.share_logo);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
d.draw(c);
Now when i add the image to the bottom (R.drawable.share_logo) it is coming on a black background. how do I create a white filled rect as background for the entire canvas?
回答1:
try this code:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
CardView card_view = (CardView) tmps.findViewById(R.id.card_view);
Bitmap bitmap = loadBitmapFromView(card_view);
来源:https://stackoverflow.com/questions/35810630/creating-image-from-view