Example usage for android.view View getDrawingCache

List of usage examples for android.view View getDrawingCache

Introduction

In this page you can find the example usage for android.view View getDrawingCache.

Prototype

@Deprecated
public Bitmap getDrawingCache(boolean autoScale) 

Source Link

Document

Returns the bitmap in which this view drawing is cached.

Usage

From source file:Main.java

/**
 * Calls {@link View#getDrawingCache(boolean)}.
 *
 * @return A bitmap representing this view, or null if caching is disabled
 *         or {@link View#buildDrawingCache(boolean)} has not been called
 *//* w  ww . j  a  v a2 s .c om*/
public static Bitmap getDrawingCache(View view, boolean autoScale) {
    return view.getDrawingCache(autoScale);
}

From source file:Main.java

/**
 * get a screen shot with size : width X height.
 */// w  w  w.  ja  v a2s  .  com
public static Bitmap getScreenShot(Activity activity, int width, int height) {
    if (activity == null || width < 1 || height < 1) {
        return null;
    }
    Window window = activity.getWindow();
    if (window == null) {
        return null;
    }
    View decorView = window.getDecorView();
    if (decorView == null) {
        return null;
    }
    decorView.setDrawingCacheEnabled(true);
    Bitmap screenShot = decorView.getDrawingCache(true);
    if (screenShot == null) {
        return null;
    }
    Matrix matrix = new Matrix();
    matrix.postScale((float) width / screenShot.getWidth(), (float) height / screenShot.getHeight());
    Bitmap drawingCache = Bitmap.createBitmap(screenShot, 0, 0, screenShot.getWidth(), screenShot.getHeight(),
            matrix, true);
    decorView.destroyDrawingCache();
    screenShot.recycle();
    return drawingCache;
}

From source file:Main.java

public static Bitmap getBitmapFromView(View paramView) {
    paramView.destroyDrawingCache();//  w  w  w .  j a va2s.  c  o m
    paramView.measure(View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0));
    paramView.layout(0, 0, paramView.getMeasuredWidth(), paramView.getMeasuredHeight());
    paramView.setDrawingCacheEnabled(true);
    return paramView.getDrawingCache(true);
}

From source file:Main.java

public static Bitmap getBitmapFromView(View view) {
    view.destroyDrawingCache();/*from   www .  java 2 s .c  o  m*/
    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.setDrawingCacheEnabled(true);
    Bitmap bitmap = view.getDrawingCache(true);
    return bitmap;
}

From source file:com.dv.Utils.Tools.java

/**
 * view?bitmap//from   w  w  w. j av a2  s. c  o m
 *
 * @param view
 * @return
 */
public static Bitmap convertViewToBitmap(View view) {
    view.destroyDrawingCache();
    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.setDrawingCacheEnabled(true);

    Bitmap bitmap = view.getDrawingCache(true);

    return bitmap;
}

From source file:com.groksolutions.grok.mobile.GrokActivity.java

/**
 * Capture the screen and return the URI of the image
 *//*www.  ja v a  2 s.  c  o m*/
private Uri takeScreenCapture(boolean isRetryOk) {
    String fileName = "GROK_" + new SimpleDateFormat("yyyyMMddhhmm'.jpg'", Locale.US).format(new Date());

    File screenShot = new File(getCacheDir(), fileName);

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    v1.invalidate();
    v1.buildDrawingCache(true);
    Bitmap bitmap = null;
    FileOutputStream fOut = null;

    try {
        bitmap = Bitmap.createBitmap(v1.getDrawingCache(true));
        v1.setDrawingCacheEnabled(false);
        fOut = new FileOutputStream(screenShot);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fOut);

    } catch (FileNotFoundException e) {
        Log.e(TAG, "Screen shot file not found", e);
    } catch (OutOfMemoryError e) {
        Log.e(TAG, "Out of Memory Error creating screenshot", e);
        // retry one time on out of memory
        if (isRetryOk) {
            return takeScreenCapture(false);
        }
        return writeTextFileToSend("screenshot.txt", "Out of Memory: Failed to generate screenshot");
    } finally {
        // recycle the bitmap on the heap to free up space and help prevent
        // out of memory errors
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
        System.gc();
        try {
            if (fOut != null) {
                fOut.flush();
                fOut.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "Error saving the screenshot file", e);
        }
    }

    return Uri.parse("content://" + getApplication().getPackageName() + "/" + fileName);
}

From source file:com.numenta.htmit.mobile.HTMITActivity.java

/**
 * Capture the screen and return the URI of the image
 *//* ww w.  java 2  s.c om*/
private Uri takeScreenCapture(boolean isRetryOk) {
    String fileName = "HTMIT_" + new SimpleDateFormat("yyyyMMddhhmm'.jpg'", Locale.US).format(new Date());

    File screenShot = new File(getCacheDir(), fileName);

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    v1.invalidate();
    v1.buildDrawingCache(true);
    Bitmap bitmap = null;
    FileOutputStream fOut = null;

    try {
        bitmap = Bitmap.createBitmap(v1.getDrawingCache(true));
        v1.setDrawingCacheEnabled(false);
        fOut = new FileOutputStream(screenShot);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fOut);

    } catch (FileNotFoundException e) {
        Log.e(TAG, "Screen shot file not found", e);
    } catch (OutOfMemoryError e) {
        Log.e(TAG, "Out of Memory Error creating screenshot", e);
        // retry one time on out of memory
        if (isRetryOk) {
            return takeScreenCapture(false);
        }
        return writeTextFileToSend("screenshot.txt", "Out of Memory: Failed to generate screenshot");
    } finally {
        // recycle the bitmap on the heap to free up space and help prevent
        // out of memory errors
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
        System.gc();
        try {
            if (fOut != null) {
                fOut.flush();
                fOut.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "Error saving the screenshot file", e);
        }
    }

    return Uri.parse("content://" + getApplication().getPackageName() + "/" + fileName);
}

From source file:com.YOMPsolutions.YOMP.mobile.YOMPActivity.java

/**
 * Capture the screen and return the URI of the image
 *//*  w  w w  .ja va  2  s  .co m*/
private Uri takeScreenCapture(boolean isRetryOk) {
    String fileName = "YOMP_" + new SimpleDateFormat("yyyyMMddhhmm'.jpg'", Locale.US).format(new Date());

    File screenShot = new File(getCacheDir(), fileName);

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    v1.invalidate();
    v1.buildDrawingCache(true);
    Bitmap bitmap = null;
    FileOutputStream fOut = null;

    try {
        bitmap = Bitmap.createBitmap(v1.getDrawingCache(true));
        v1.setDrawingCacheEnabled(false);
        fOut = new FileOutputStream(screenShot);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fOut);

    } catch (FileNotFoundException e) {
        Log.e(TAG, "Screen shot file not found", e);
    } catch (OutOfMemoryError e) {
        Log.e(TAG, "Out of Memory Error creating screenshot", e);
        // retry one time on out of memory
        if (isRetryOk) {
            return takeScreenCapture(false);
        }
        return writeTextFileToSend("screenshot.txt", "Out of Memory: Failed to generate screenshot");
    } finally {
        // recycle the bitmap on the heap to free up space and help prevent
        // out of memory errors
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
        System.gc();
        try {
            if (fOut != null) {
                fOut.flush();
                fOut.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "Error saving the screenshot file", e);
        }
    }

    return Uri.parse("content://" + getApplication().getPackageName() + "/" + fileName);
}

From source file:com.numenta.taurus.TaurusBaseActivity.java

/**
 * Capture the screen and return the URI of the image
 *//*from  w  w w.  j  a  v a  2 s .  co  m*/
private Uri takeScreenCapture(boolean isRetryOk) {
    String fileName = "FILE_" + new SimpleDateFormat("yyyyMMddhhmm'.jpg'", Locale.US).format(new Date());

    File screenShot = new File(getCacheDir(), fileName);

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    v1.invalidate();
    v1.buildDrawingCache(true);
    Bitmap bitmap = null;
    FileOutputStream fOut = null;

    try {
        bitmap = Bitmap.createBitmap(v1.getDrawingCache(true));
        v1.setDrawingCacheEnabled(false);
        fOut = new FileOutputStream(screenShot);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fOut);

    } catch (FileNotFoundException e) {
        Log.e(TAG, "Screen shot file not found", e);
    } catch (OutOfMemoryError e) {
        Log.e(TAG, "Out of Memory Error creating screenshot", e);
        // retry one time on out of memory
        if (isRetryOk) {
            return takeScreenCapture(false);
        }
        return writeTextFileToSend("screenshot.txt", "Out of Memory: Failed to generate screenshot");
    } finally {
        // recycle the bitmap on the heap to free up space and help prevent
        // out of memory errors
        if (bitmap != null) {
            bitmap.recycle();
            //noinspection UnusedAssignment
            bitmap = null;
        }
        System.gc();
        try {
            if (fOut != null) {
                fOut.flush();
                fOut.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "Error saving the screenshot file", e);
        }
    }

    return Uri.parse("content://" + getApplication().getPackageName() + "/" + fileName);
}

From source file:org.appcelerator.titanium.util.TiUIHelper.java

/**
 * Draw the view into a bitmap.//from w  ww  . j a  v  a2 s  . co m
 */
public static Bitmap getViewBitmap(final View v) {
    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();

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

    if (willNotCache || color != 0) {
        v.setWillNotCacheDrawing(false);
        v.setDrawingCacheBackgroundColor(0);
        v.destroyDrawingCache();
    }

    boolean isDrawinCacheEnabled = v.isDrawingCacheEnabled();

    Bitmap bitmap = null;

    try {
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(v.getDrawingCache(true));
    } catch (Exception e) {
        bitmap = null;
    }
    v.setDrawingCacheEnabled(isDrawinCacheEnabled);

    // Restore the view
    if (willNotCache || color != 0) {
        v.destroyDrawingCache();
        v.setDrawingCacheBackgroundColor(color);
        v.setWillNotCacheDrawing(willNotCache);
    }

    return bitmap;
}