user2908062
user2908062

Reputation:

Converting a view to Bitmap without displaying (with setHorizontallyScrollyng) in Android?

In my code, I have a LinearLayout with many TextViews (like a table). And I try to convert this to Bitmap. there is my code:

    public static Bitmap convertToBitmap(LinearLayout view){
    view.measure(LinearLayout.LayoutParams.WRAP_CONTENT,
                 LinearLayout.LayoutParams.WRAP_CONTENT);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                                        view.getHeight(),
                                        Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    view.draw(c);
    return bitmap;
}

But, there is a problem. Views witch has setSingleLine(true) not display text in bitmap. Exactly, the problem is in setHorizontallyScrolling(true), which is calling in setSingleLine code... I try use .setScrollTo(X), but what exactly X must be. X = 0 - not works.

Is there any, who knows how fix it?

EDIT: TextViews has fixed Width. EDIT: Problem is only for TextViews with Gravity = Center. With gravity Left all's ok.

Upvotes: 0

Views: 812

Answers (1)

Roger Alien
Roger Alien

Reputation: 3060

Try one of this code? It's a bit different from the one you posted above.

/**
 * this actually also provided by Romain Guy, but let's try another for performance improvements
 */
public static Bitmap getBitmapFromView(View view, int width, int height) {
    view.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    // Build the Drawing Cache
    view.buildDrawingCache();

    // Create Bitmap
    Bitmap drawingCache = view.getDrawingCache();
    if (drawingCache == null) {
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(drawingCache);
    drawingCache.recycle();
    view.setDrawingCacheEnabled(false);
    return bitmap;
}

/**
 * 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;
}

Upvotes: 1

Related Questions