简体   繁体   中英

Convert a view to Bitmap image in android

I'm fairly new to Android and looking for some help in converting a view to Bitmap image. I have an Activity where I created a RelativeLayout and added 2 TextViews one on top and one at the bottom. The Activity displays fine if RelativeLayout itself is displayed. I'm trying to convert this view to a Bitmap and display as ImageView (added to a LinearLayout ) instead of displaying the RelativeLayout . But the display doesn't seem to be preserving the layout of the View , instead it is wrapping up the elements together and displaying in the image.

Can someone please suggest what is going wrong in this? Here is the simple code I have written

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

    LinearLayout finalImage = new LinearLayout(this);
    finalImage.setLayoutParams(lp);

    RelativeLayout main = new RelativeLayout(this);
    main.setLayoutParams(lp);

    TextView tv = new TextView(this);
    tv.setTextColor(Color.RED);
    tv.setText("Top Text Content");
    tv.setGravity(Gravity.CENTER);
    tv.setId(1);
    lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    main.addView(tv,lp);

    TextView headingView = new TextView(this);
    headingView.setTextColor(Color.RED);
    headingView.setPadding(15, 10, 10, 10);
    headingView.setTextSize(20);
    headingView.setText("Bottom Text Content");
    headingView.setGravity(Gravity.CENTER);
    headingView.setId(2);
    lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    main.addView(headingView,lp);


    main.setDrawingCacheEnabled(true);

 // this is the important code :)  
 // Without it the view will have a dimension of 0,0 and the bitmap will be null          
    main.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    main.layout(0, 0, main.getMeasuredWidth(), main.getMeasuredHeight()); 

    main.buildDrawingCache(true);

 Bitmap returnedBitmap = Bitmap.createBitmap(main.getDrawingCache());
 main.setDrawingCacheEnabled(false); // clear drawing cache


    ImageView iv = new ImageView(this);
    iv.setImageBitmap(returnedBitmap);

    finalImage.addView(iv);

    //setContentView(main);

    setContentView(finalImage);


}

This snippet works fine for me:

/**
 * This method provided by Romain Guy, so it should do the job better, especially it includes case for listViews
 */
public static Bitmap getBitmapFromView(View view, int width, int height) {

    //Pre-measure the view so that height and width don't remain null.
    view.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
    //Assign a size and position to the view and all of its descendants
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    // Create bitmap
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);

    //Create a canvas with the specified bitmap to draw into
    Canvas canvas = new Canvas(bitmap);

    // if it's scrollView we get gull size
    canvas.translate(-view.getScrollX(), -view.getScrollY());
    //Render this view (and all of its children) to the given Canvas
    view.draw(canvas);
    return bitmap;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM