get View Bitmap with background color - Android User Interface

Android examples for User Interface:View Bitmap

Description

get View Bitmap with background color

Demo Code


//package com.java2s;
import android.graphics.Bitmap;

import android.util.Log;
import android.view.View;

public class Main {
    private static final String TAG = "ViewUtils";

    public static Bitmap getViewBitmap(View v, int backgroundColor) {
        v.clearFocus();//from w w  w . ja v a 2 s.  c  o m
        v.setPressed(false);

        boolean willNotCache = v.willNotCacheDrawing();
        v.setWillNotCacheDrawing(false);

        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        int color = v.getDrawingCacheBackgroundColor();
        v.setDrawingCacheBackgroundColor(backgroundColor);

        if (color != 0) {
            v.destroyDrawingCache();
        }
        v.buildDrawingCache();
        Bitmap cacheBitmap = v.getDrawingCache();
        if (cacheBitmap == null) {
            Log.e(TAG, "failed getViewBitmap(" + v + ")",
                    new RuntimeException());
            return null;
        }

        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
        // Restore the view
        v.destroyDrawingCache();
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);

        return bitmap;
    }
}

Related Tutorials