Example usage for android.view View setDrawingCacheEnabled

List of usage examples for android.view View setDrawingCacheEnabled

Introduction

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

Prototype

@Deprecated
public void setDrawingCacheEnabled(boolean enabled) 

Source Link

Document

Enables or disables the drawing cache.

Usage

From source file:com.bobomee.android.common.util.ScreenUtil.java

/**
 * http://stackoverflow.com/questions/12742343/android-get-screenshot-of-all-listview-items
 *//*  www . ja v  a 2  s.co  m*/
public static Bitmap shotListView(ListView listview) {

    ListAdapter adapter = listview.getAdapter();
    int itemscount = adapter.getCount();
    int allitemsheight = 0;
    List<Bitmap> bmps = new ArrayList<Bitmap>();

    for (int i = 0; i < itemscount; i++) {

        View childView = adapter.getView(i, null, listview);
        childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
        childView.setDrawingCacheEnabled(true);
        childView.buildDrawingCache();
        bmps.add(childView.getDrawingCache());
        allitemsheight += childView.getMeasuredHeight();
    }

    Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight,
            Bitmap.Config.ARGB_8888);
    Canvas bigcanvas = new Canvas(bigbitmap);

    Paint paint = new Paint();
    int iHeight = 0;

    for (int i = 0; i < bmps.size(); i++) {
        Bitmap bmp = bmps.get(i);
        bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
        iHeight += bmp.getHeight();

        bmp.recycle();
        bmp = null;
    }

    return bigbitmap;
}

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

/**
 * view?bitmap//from   ww  w. java 2 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:ch.uzh.supersede.feedbacklibrary.utils.Utils.java

@NonNull
private static String captureScreenshot(final Activity activity) {
    // Create the 'Screenshots' folder if it does not already exist
    File screenshotDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            SCREENSHOTS_DIR_NAME);//from w w  w  .j a v  a2  s.c o  m
    screenshotDir.mkdirs();

    // Image name 'Screenshot_YearMonthDay-HourMinuteSecondMillisecond.png'
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    String imageName = "Screenshot_" + calendar.get(Calendar.YEAR) + (calendar.get(Calendar.MONTH) + 1)
            + calendar.get(Calendar.DAY_OF_MONTH);
    imageName += "-" + calendar.get(Calendar.HOUR_OF_DAY) + calendar.get(Calendar.MINUTE)
            + calendar.get(Calendar.SECOND) + calendar.get(Calendar.MILLISECOND) + ".png";
    File screenshotFile = new File(screenshotDir, imageName);

    // Create the screenshot file
    try {
        if (screenshotFile.exists()) {
            screenshotFile.delete();
        }
        screenshotFile.createNewFile();
    } catch (IOException e) {
        Log.e(TAG, "Failed to create a new file", e);
    }

    // Capture the current screen
    View rootView = activity.getWindow().getDecorView().getRootView();
    rootView.setDrawingCacheEnabled(true);
    Bitmap imageBitmap = Bitmap.createBitmap(rootView.getDrawingCache());
    rootView.setDrawingCacheEnabled(false);

    FileOutputStream fos;
    try {
        fos = new FileOutputStream(screenshotFile);
        imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        Log.e(TAG, "Failed to write the bitmap to the file", e);
    }

    // Add the screenshot image to the Media Provider's database
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(screenshotFile);
    mediaScanIntent.setData(contentUri);
    activity.sendBroadcast(mediaScanIntent);

    return screenshotFile.getAbsolutePath();
}

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

public static Bitmap takeViewScreenshot(View view) {
    if (view == null) {
        return null;
    }//from   w w w.  ja  v  a2s.  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   ww  w .  ja  va2s .  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:org.apache.cordova.Screenshot.java

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    try {//w  w  w. j a  va  2s. 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);
    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);//from  www.  ja  va 2 s  .c  o m
    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;//  ww  w . j a  va 2s  .  c  o  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//from w w  w.  ja v a  2s .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);
}