Unable to convert viewgroup into bitmap in Android
I am developing an Android app that generates image. Before generate image, user will drag and drop views onto RelativeLayout. Then I need to convert that relative layout with child views into bitmap. So I am adding view dynamically to a relative layout and then convert it into bitmap. But my code giving me error.
I am adding views to relative layout like this
relativeLayout.add(imageView);
relativeLayout.add(textView);
//other steps and so on
This is my function to convert RelativeLayout into Bitmap
private Bitmap convertTemplateViewToBitmap(RelativeLayout v)
{
v.measure(View.MeasureSpec.makeMeasureSpec(v.getLayoutParams().width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(v.getLayoutParams().height, View.MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}
So I call that button in a button click passing relative layout as parameter. When I click, it throws error.
This is the error message in logcat
FATAL EXCEPTION: main Process: memegenerator.waiyanhein.com.memegenerator, PID: 19993 java.lang.IllegalArgumentException: width and height must be > 0 at android.graphics.Bitmap.createBitmap(Bitmap.java:829) at android.graphics.Bitmap.createBitmap(Bitmap.java:808) at android.graphics.Bitmap.createBitmap(Bitmap.java:775) at memegenerator.waiyanhein.com.memegenerator.CreateMemeActivity.convertTemplateViewToBitmap(CreateMemeActivity.java:898) at memegenerator.waiyanhein.com.memegenerator.CreateMemeActivity.saveBottomItemClick(CreateMemeActivity.java:285) at memegenerator.waiyanhein.com.memegenerator.CreateMemeActivity.access$800(CreateMemeActivity.java:51) at memegenerator.waiyanhein.com.memegenerator.CreateMemeActivity$2.onTabSelected(CreateMemeActivity.java:251) at com.roughike.bottombar.BottomBar.updateSelectedTab(BottomBar.java:717) at com.roughike.bottombar.BottomBar.handleClick(BottomBar.java:685) at com.roughike.bottombar.BottomBar.onClick(BottomBar.java:656) at android.view.View.p erformClick(View.java:5204) at android.view.View$PerformClick.run(View.java:21153) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
What is wrong with my code? How can I convert a viewgroup with childs into a bitmap in Android?
here is my solution:
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
Enjoy :)
链接地址: http://www.djcxy.com/p/33662.html上一篇: Android与内存位图不兼容
下一篇: 无法将视图组转换为Android中的位图