Creating Image from View

ⅰ亾dé卋堺 提交于 2019-12-08 13:56:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!