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() 

Source Link

Document

Calling this method is equivalent to calling getDrawingCache(false).

Usage

From source file:com.jungle.base.utils.MiscUtils.java

public static Bitmap takeViewScreenshot(View view) {
    if (view == null) {
        return null;
    }/* w  w w  .  ja  v  a2  s .c o m*/

    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    return bitmap;
}

From source file:im.vector.util.BugReporter.java

/**
 * Take a screenshot of the display.//from  w  w w  .j a va  2  s  .c  o  m
 *
 * @return the screenshot
 */
private static Bitmap takeScreenshot() {
    // sanity check
    if (VectorApp.getCurrentActivity() == null) {
        return null;
    }
    // get content view
    View contentView = VectorApp.getCurrentActivity().findViewById(android.R.id.content);
    if (contentView == null) {
        Log.e(LOG_TAG,
                "Cannot find content view on " + VectorApp.getCurrentActivity() + ". Cannot take screenshot.");
        return null;
    }

    // get the root view to snapshot
    View rootView = contentView.getRootView();
    if (rootView == null) {
        Log.e(LOG_TAG,
                "Cannot find root view on " + VectorApp.getCurrentActivity() + ". Cannot take screenshot.");
        return null;
    }
    // refresh it
    rootView.setDrawingCacheEnabled(false);
    rootView.setDrawingCacheEnabled(true);

    try {
        return rootView.getDrawingCache();
    } catch (OutOfMemoryError oom) {
        Log.e(LOG_TAG, "Cannot get drawing cache for " + VectorApp.getCurrentActivity() + " OOM.");
    } catch (Exception e) {
        Log.e(LOG_TAG, "Cannot get snapshot of screen: " + e);
    }
    return null;
}

From source file:Main.java

public static Bitmap convertViewToBitmap(View comBitmap, int width, int height) {
    Bitmap bitmap = null;//w  ww  .j a v  a 2  s.co m
    if (comBitmap != null) {
        comBitmap.clearFocus();
        comBitmap.setPressed(false);

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

        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        int color = comBitmap.getDrawingCacheBackgroundColor();
        comBitmap.setDrawingCacheBackgroundColor(0);
        float alpha = comBitmap.getAlpha();
        comBitmap.setAlpha(1.0f);

        if (color != 0) {
            comBitmap.destroyDrawingCache();
        }

        int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
        comBitmap.measure(widthSpec, heightSpec);
        comBitmap.layout(0, 0, width, height);

        comBitmap.buildDrawingCache();
        Bitmap cacheBitmap = comBitmap.getDrawingCache();
        if (cacheBitmap == null) {
            Log.e("view.ProcessImageToBlur", "failed getViewBitmap(" + comBitmap + ")", new RuntimeException());
            return null;
        }
        bitmap = Bitmap.createBitmap(cacheBitmap);
        // Restore the view
        comBitmap.setAlpha(alpha);
        comBitmap.destroyDrawingCache();
        comBitmap.setWillNotCacheDrawing(willNotCache);
        comBitmap.setDrawingCacheBackgroundColor(color);
    }
    return bitmap;
}

From source file:org.apache.cordova.Screenshot.java

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    try {/*from www.jav  a  2 s. c o m*/
        fileName = args.getString(0);
        Log.d(TAG, "execute(). fileName: " + fileName);
    } catch (Exception e) {
        Log.e("Screenshot error", e.toString());
    }

    // starting on ICS, some WebView methods
    // can only be called on UI threads
    final Plugin that = this;
    final String id = callbackId;
    super.cordova.getActivity().runOnUiThread(new Runnable() {
        @Override
        //@TargetApi(Build.VERSION_CODES.FROYO)
        public void run() {
            View view = webView.getRootView();
            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);

            try {
                File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
                if (!folder.exists()) {
                    folder.mkdirs();
                }
                File f = new File(folder, fileName);
                FileOutputStream fos = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

                // Return new file's absolute path, so success callback can pass it to the Share plugin
                that.success(new PluginResult(PluginResult.Status.OK, f.getAbsolutePath()), id);
            } catch (IOException e) {
                that.success(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage()), id);
            }
        }
    });

    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
    result.setKeepCallback(true);
    return result;
}

From source file:com.example.android.animationsdemo.RecentQueryActivity.java

public void saveView(View view) {
    view.setDrawingCacheEnabled(true);
    Bitmap bmScreen = view.getDrawingCache();
    saveImage(bmScreen);
}

From source file:com.snu_artoon.arwebtoonplayer.WebtoonView.WebtoonViewActivity.java

private void captureScreen() {
    View v = getWindow().getDecorView().getRootView();
    v.setDrawingCacheEnabled(true);/*from  w ww  .  j ava  2s. co m*/
    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    try {
        FileOutputStream fos = new FileOutputStream(
                new File(Environment.getExternalStorageDirectory().toString(),
                        "SCREEN" + System.currentTimeMillis() + ".png"));
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.darktalker.cordova.screenshot.Screenshot.java

private Bitmap getBitmap() {
    Bitmap bitmap = null;//from   w w  w .  j a v a  2  s.co m

    boolean isCrosswalk = false;
    try {
        Class.forName("org.crosswalk.engine.XWalkWebViewEngine");
        isCrosswalk = true;
    } catch (Exception e) {
    }

    if (isCrosswalk) {
        try {

            TextureView textureView = findXWalkTextureView((ViewGroup) webView.getView());
            if (textureView != null) {
                bitmap = textureView.getBitmap();
                return bitmap;
            }
        } catch (Exception e) {
        }
    }

    View view = webView.getView().getRootView();
    view.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    return bitmap;
}

From source file:devlight.io.library.CutIntoLayout.java

@SuppressLint("DrawAllocation")
@Override/*  w w  w.  java 2 s  .  c  o  m*/
protected void onLayout(final boolean changed, final int left, final int top, final int right,
        final int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    if (getChildCount() > 1)
        throw new IllegalArgumentException(getResources().getString(R.string.child_exception));

    final View child = getChildAt(0);

    child.setVisibility(VISIBLE);
    child.setDrawingCacheEnabled(true);
    child.buildDrawingCache();

    final Bitmap drawingCache = child.getDrawingCache();
    if (drawingCache == null)
        return;

    // Obtain child screenshot
    mChildBitmap = Bitmap.createBitmap(drawingCache);
    drawingCache.recycle();

    // Obtain child offset
    mChildLeft = child.getLeft();
    mChildTop = child.getTop();

    child.setDrawingCacheEnabled(false);
    child.setVisibility(GONE);
}

From source file:uk.co.placona.selfie.Selfie.java

/**
 * This method is responsible for taking the screenshot and creating a file
 *
 * @param activity Activity used by Selfie.
 * @return {@code true} if the screenshot was taken, false otherwise.
 *//* www . j  av  a 2 s .  co  m*/
private boolean takeScreenShot(Activity activity) {
    Date now = new Date();
    android.text.format.DateFormat.format(fileFormat, now);

    // create bitmap screen capture
    View v1 = activity.getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    // image naming and path to include sd card appending name you choose for file
    File imageFile = new File(path, now + ".jpg");

    try {
        FileOutputStream outputStream = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        return false;
    }

    return true;
}

From source file:org.apache.cordova.screenshot.Screenshot.java

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    // starting on ICS, some WebView methods
    // can only be called on UI threads
    final String format = (String) args.get(0);
    final Integer quality = (Integer) args.get(1);
    if (action.equals("saveScreenshot")) {
        super.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override//  w  ww.j a va 2 s. c o  m
            public void run() {
                View view = webView.getRootView();
                try {
                    if (format.equals("png") || format.equals("jpg")) {
                        view.setDrawingCacheEnabled(true);
                        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
                        view.setDrawingCacheEnabled(false);
                        File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
                        if (!folder.exists()) {
                            folder.mkdirs();
                        }

                        File f = new File(folder, "screenshot_" + System.currentTimeMillis() + "." + format);

                        FileOutputStream fos = new FileOutputStream(f);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();

                        if (format.equals("png")) {
                            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                        }
                        if (format.equals("jpg")) {
                            bitmap.compress(Bitmap.CompressFormat.JPEG, quality == null ? 100 : quality,
                                    stream);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, quality == null ? 100 : quality, fos);
                        }

                        byte[] byteArray = stream.toByteArray();
                        String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);

                        JSONObject jsonRes = new JSONObject();
                        jsonRes.put("filePath", f.getAbsolutePath());
                        jsonRes.put("based64Content", encodedImage);
                        PluginResult result = new PluginResult(PluginResult.Status.OK, jsonRes);
                        callbackContext.sendPluginResult(result);
                    } else {
                        callbackContext.error("format " + format + " not found");

                    }

                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());

                } catch (IOException e) {
                    callbackContext.error(e.getMessage());

                }
            }
        });
        return true;
    }
    callbackContext.error("action not found");
    return false;

}