Example usage for android.view View buildDrawingCache

List of usage examples for android.view View buildDrawingCache

Introduction

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

Prototype

@Deprecated
public void buildDrawingCache(boolean autoScale) 

Source Link

Document

Forces the drawing cache to be built if the drawing cache is invalid.

If you call #buildDrawingCache() manually without calling #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true) , you should cleanup the cache by calling #destroyDrawingCache() afterwards.

Note about auto scaling in compatibility mode: When auto scaling is not enabled, this method will create a bitmap of the same size as this view.

Usage

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

/**
 * Capture the screen and return the URI of the image
 *//* w  w w .  j  a  va  2s .co  m*/
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
 *///from  w  ww.  ja v  a  2s  .  c o  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
 *//*  w ww. j  a  va  2s. c  om*/
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.openqa.selendroid.server.model.AbstractSelendroidDriver.java

@Override
public byte[] takeScreenshot() {
    final View view = serverInstrumentation.getRootView();
    if (view == null) {
        throw new SelendroidException("No open windows.");
    }/*from w w w  .  j  av a  2 s .c o m*/
    done = false;
    long end = System.currentTimeMillis() + serverInstrumentation.getAndroidWait().getTimeoutInMillis();
    final byte[][] rawPng = new byte[1][1];
    ServerInstrumentation.getInstance().getCurrentActivity().runOnUiThread(new Runnable() {
        public void run() {
            synchronized (syncObject) {
                Bitmap raw;
                view.setDrawingCacheEnabled(true);
                view.buildDrawingCache(true);
                raw = Bitmap.createBitmap(view.getDrawingCache());
                view.setDrawingCacheEnabled(false);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                if (!raw.compress(Bitmap.CompressFormat.PNG, 100, stream)) {
                    throw new RuntimeException("Error while compressing screenshot image.");
                }
                try {
                    stream.flush();
                    stream.close();
                } catch (IOException e) {
                    throw new RuntimeException("I/O Error while capturing screenshot: " + e.getMessage());
                } finally {
                    IOUtils.closeQuietly(stream);
                }
                rawPng[0] = stream.toByteArray();
                done = true;
                syncObject.notify();
            }
        }
    });

    waitForDone(end, serverInstrumentation.getAndroidWait().getTimeoutInMillis(), "Failed to take screenshot.");
    return rawPng[0];
}

From source file:org.catrobat.catroid.ui.fragment.AddBrickFragment.java

private void clickedOnUserBrick(final UserBrick clickedBrick, View view) {
    final Context context = getActivity();

    final List<CharSequence> items = new ArrayList<CharSequence>();

    items.add(context.getText(R.string.brick_context_dialog_add_to_script));

    items.add(context.getText(R.string.brick_context_dialog_edit_brick));

    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    boolean drawingCacheEnabled = view.isDrawingCacheEnabled();
    view.setDrawingCacheEnabled(true);/*from w ww.  j a  v a  2  s.  c o m*/
    view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
    view.buildDrawingCache(true);

    if (view.getDrawingCache() != null) {
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(drawingCacheEnabled);

        ImageView imageView = getGlowingBorder(bitmap);
        builder.setCustomTitle(imageView);
    }

    builder.setItems(items.toArray(new CharSequence[items.size()]), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            CharSequence clickedItemText = items.get(item);
            if (clickedItemText.equals(context.getText(R.string.brick_context_dialog_add_to_script))) {
                addBrickToScript(clickedBrick);
            } else if (clickedItemText.equals(context.getText(R.string.brick_context_dialog_edit_brick))) {
                launchBrickScriptActivityOnBrick(context, clickedBrick);
            }
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

From source file:hku.fyp14017.blencode.ui.fragment.AddBrickFragment.java

private void clickedOnUserBrick(final UserBrick clickedBrick, View view) {
    final Context context = getActivity();

    final List<CharSequence> items = new ArrayList<CharSequence>();

    items.add(context.getText(hku.fyp14017.blencode.R.string.brick_context_dialog_add_to_script));

    items.add(context.getText(hku.fyp14017.blencode.R.string.brick_context_dialog_edit_brick));

    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    boolean drawingCacheEnabled = view.isDrawingCacheEnabled();
    view.setDrawingCacheEnabled(true);/*ww w. j av  a2  s.  c o m*/
    view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
    view.buildDrawingCache(true);

    if (view.getDrawingCache() != null) {
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(drawingCacheEnabled);

        ImageView imageView = getGlowingBorder(bitmap);
        builder.setCustomTitle(imageView);
    }

    builder.setItems(items.toArray(new CharSequence[items.size()]), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            CharSequence clickedItemText = items.get(item);
            if (clickedItemText.equals(
                    context.getText(hku.fyp14017.blencode.R.string.brick_context_dialog_add_to_script))) {
                addBrickToScript(clickedBrick);
            } else if (clickedItemText
                    .equals(context.getText(hku.fyp14017.blencode.R.string.brick_context_dialog_edit_brick))) {
                launchBrickScriptActivityOnBrick(context, clickedBrick);
            }

        }

    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

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

/**
 * Draw the view into a bitmap./*from   w  w  w  . java 2  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;
}

From source file:com.musenkishi.wally.fragments.LatestFragment.java

private void setupAdapter() {
    imagesAdapter.setOnGetViewListener(new RecyclerImagesAdapter.OnGetViewListener() {
        @Override/*from   w w w .  j  a v a  2  s.com*/
        public void onBindView(int position) {
            getMoreImagesIfNeeded(position, imagesAdapter.getItemCount());
        }
    });
    imagesAdapter.setOnItemClickListener(new RecyclerImagesAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Image image = (Image) imagesAdapter.getItem(position);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(image.imagePageURL()), view.getContext(),
                    ImageDetailsActivity.class);

            ImageView thumbnailImageView = (ImageView) view.findViewById(R.id.thumb_image_view);

            Bitmap thumb = null;

            intent.putExtra(ImageDetailsActivity.INTENT_EXTRA_IMAGE, image);

            if (thumbnailImageView != null && thumbnailImageView.getDrawable() != null
                    && thumbnailImageView.getDrawable() instanceof GlideBitmapDrawable) {
                GlideBitmapDrawable glideBitmapDrawable = (GlideBitmapDrawable) thumbnailImageView
                        .getDrawable();
                thumb = glideBitmapDrawable.getBitmap();
            } else if (thumbnailImageView != null && thumbnailImageView.getDrawable() != null
                    && thumbnailImageView.getDrawable() instanceof TransitionDrawable) {
                GlideBitmapDrawable squaringDrawable = (GlideBitmapDrawable) ((TransitionDrawable) thumbnailImageView
                        .getDrawable()).getDrawable(1);
                thumb = squaringDrawable.getBitmap();
            }
            WallyApplication.setBitmapThumb(thumb);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                view.buildDrawingCache(true);
                Bitmap drawingCache = view.getDrawingCache(true);
                Bundle bundle = ActivityOptions.makeThumbnailScaleUpAnimation(view, drawingCache, 0, 0)
                        .toBundle();
                getActivity().startActivityForResult(intent, REQUEST_CODE, bundle);
            } else {
                startActivityForResult(intent, REQUEST_CODE);
            }
        }
    });
}

From source file:com.musenkishi.wally.fragments.RandomImagesFragment.java

private void setupAdapter() {
    imagesAdapter.setOnGetViewListener(new RecyclerImagesAdapter.OnGetViewListener() {
        @Override//from w  w  w. j a  v a  2  s . c  o  m
        public void onBindView(int position) {
            int defaultNumberOfItemsPerPage = NetworkDataProvider.THUMBS_PER_PAGE;
            boolean shouldLoadMore = position >= imagesAdapter.getItemCount()
                    - (defaultNumberOfItemsPerPage / 2);
            if (shouldLoadMore && !isLoading && imagesAdapter.getItemCount() > 0) {
                getImages(++currentPage, null);
            }
        }
    });
    imagesAdapter.setOnItemClickListener(new RecyclerImagesAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Image image = (Image) imagesAdapter.getItem(position);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(image.imagePageURL()), view.getContext(),
                    ImageDetailsActivity.class);

            ImageView thumbnailImageView = (ImageView) view.findViewById(R.id.thumb_image_view);

            Bitmap thumb = null;

            intent.putExtra(ImageDetailsActivity.INTENT_EXTRA_IMAGE, image);

            if (thumbnailImageView != null && thumbnailImageView.getDrawable() != null
                    && thumbnailImageView.getDrawable() instanceof GlideBitmapDrawable) {
                GlideBitmapDrawable glideBitmapDrawable = (GlideBitmapDrawable) thumbnailImageView
                        .getDrawable();
                thumb = glideBitmapDrawable.getBitmap();
            } else if (thumbnailImageView != null && thumbnailImageView.getDrawable() != null
                    && thumbnailImageView.getDrawable() instanceof TransitionDrawable) {
                GlideBitmapDrawable squaringDrawable = (GlideBitmapDrawable) ((TransitionDrawable) thumbnailImageView
                        .getDrawable()).getDrawable(1);
                thumb = squaringDrawable.getBitmap();
            }
            WallyApplication.setBitmapThumb(thumb);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                view.buildDrawingCache(true);
                Bitmap drawingCache = view.getDrawingCache(true);
                Bundle bundle = ActivityOptions.makeThumbnailScaleUpAnimation(view, drawingCache, 0, 0)
                        .toBundle();
                getActivity().startActivityForResult(intent, REQUEST_CODE, bundle);
            } else {
                startActivityForResult(intent, REQUEST_CODE);
            }
        }
    });
}

From source file:com.musenkishi.wally.fragments.SearchFragment.java

private void setupAdapter() {

    imagesAdapter.setOnGetViewListener(new RecyclerImagesAdapter.OnGetViewListener() {
        @Override//ww w  . j av a 2s.c om
        public void onBindView(int position) {
            if (gridView.getAdapter() != null) {
                getMoreImagesIfNeeded(position, imagesAdapter.getItemCount());
            }
        }
    });

    imagesAdapter.setOnItemClickListener(new RecyclerImagesAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Image image = (Image) imagesAdapter.getItem(position);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(image.imagePageURL()), view.getContext(),
                    ImageDetailsActivity.class);

            ImageView thumbnailImageView = (ImageView) view.findViewById(R.id.thumb_image_view);

            Bitmap thumb = null;

            intent.putExtra(ImageDetailsActivity.INTENT_EXTRA_IMAGE, image);

            if (thumbnailImageView != null && thumbnailImageView.getDrawable() != null
                    && thumbnailImageView.getDrawable() instanceof GlideBitmapDrawable) {
                GlideBitmapDrawable glideBitmapDrawable = (GlideBitmapDrawable) thumbnailImageView
                        .getDrawable();
                thumb = glideBitmapDrawable.getBitmap();
            } else if (thumbnailImageView != null && thumbnailImageView.getDrawable() != null
                    && thumbnailImageView.getDrawable() instanceof TransitionDrawable) {
                GlideBitmapDrawable squaringDrawable = (GlideBitmapDrawable) ((TransitionDrawable) thumbnailImageView
                        .getDrawable()).getDrawable(1);
                thumb = squaringDrawable.getBitmap();
            }
            WallyApplication.setBitmapThumb(thumb);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                view.buildDrawingCache(true);
                Bitmap drawingCache = view.getDrawingCache(true);
                Bundle bundle = ActivityOptions.makeThumbnailScaleUpAnimation(view, drawingCache, 0, 0)
                        .toBundle();
                getActivity().startActivityForResult(intent, REQUEST_CODE, bundle);
            } else {
                startActivityForResult(intent, REQUEST_CODE);
            }
        }
    });
}