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.bobomee.android.common.util.ScreenUtil.java

/**
 * shot the current screen ,with the status and navigationbar*
 *///from  ww  w.j  a v a  2 s.c om
public static Bitmap ShotActivity$WithoutStatus$WithoutNavigationBar(Activity ctx) {
    int statusH = getStatusH(ctx);
    int navigationBarH = getNavigationBarHeight(ctx);

    View view = ctx.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();

    Bitmap bp = Bitmap.createBitmap(view.getDrawingCache(), 0, statusH, view.getMeasuredWidth(),
            view.getMeasuredHeight() - statusH - navigationBarH);

    view.setDrawingCacheEnabled(false);
    view.destroyDrawingCache();

    return bp;
}

From source file:com.simas.vc.helpers.Utils.java

public static Bitmap screenshot(View v) {
    v.setDrawingCacheEnabled(true);//from www .  j a  v a 2  s.c  om
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);

    return b;
}

From source file:Main.java

public static Bitmap getScaledScreenshot(final Activity activity, int scaleWidth, int scaleHeight,
        boolean relativeScaleIfTrue) {
    final View someView = activity.findViewById(android.R.id.content);
    final View rootView = someView.getRootView();
    final boolean originalCacheState = rootView.isDrawingCacheEnabled();
    rootView.setDrawingCacheEnabled(true);
    rootView.buildDrawingCache(true);/*from w  w w  . ja v  a2  s .c  o m*/

    // We could get a null or zero px bitmap if the rootView hasn't been measured
    // appropriately, or we grab it before layout.
    // This is ok, and we should handle it gracefully.
    final Bitmap original = rootView.getDrawingCache();
    Bitmap scaled = null;
    if (null != original && original.getWidth() > 0 && original.getHeight() > 0) {
        if (relativeScaleIfTrue) {
            scaleWidth = original.getWidth() / scaleWidth;
            scaleHeight = original.getHeight() / scaleHeight;
        }
        if (scaleWidth > 0 && scaleHeight > 0) {
            try {
                scaled = Bitmap.createScaledBitmap(original, scaleWidth, scaleHeight, false);
            } catch (OutOfMemoryError error) {
                Log.i(LOGTAG, "Not enough memory to produce scaled image, returning a null screenshot");
            }
        }
    }
    if (!originalCacheState) {
        rootView.setDrawingCacheEnabled(false);
    }
    return scaled;
}

From source file:Main.java

public static Bitmap getViewBitmap(View v) {
    v.setDrawingCacheEnabled(true);//  w  w  w.  java 2s  .  c  om

    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will
    // be null
    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);
    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false); // clear drawing cache

    return bmp;
}

From source file:Main.java

public static Bitmap view2Bitmap(View view) {
    Bitmap bitmap = null;//from   w  w  w.  j  av a  2  s .  c  o  m
    try {
        if (view != null) {
            view.setDrawingCacheEnabled(true);
            view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
            view.buildDrawingCache();
            bitmap = view.getDrawingCache();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Drawable convertViewToDrawable(View view) {
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(spec, spec);//  ww  w.j a v  a2s. c o  m
    view.layout(UPPER_LEFT_X, UPPER_LEFT_Y, view.getMeasuredWidth(), view.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(c);
    view.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = view.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    view.destroyDrawingCache();
    return new BitmapDrawable(viewBmp);
}

From source file:com.frostwire.android.gui.util.UIUtils.java

/**
 * Takes a screenshot of the given view//from  w  w  w . j  ava2s .  c om
 * @return File with jpeg of the screenshot taken. null if there was a problem.
 */
public static File takeScreenshot(View view) {
    view.setDrawingCacheEnabled(true);
    try {
        Thread.sleep(300);
    } catch (Throwable t) {
    }
    Bitmap drawingCache = null;
    try {
        drawingCache = view.getDrawingCache();
    } catch (Throwable ignored) {
    }
    Bitmap screenshotBitmap = null;
    if (drawingCache != null) {
        try {
            screenshotBitmap = Bitmap.createBitmap(drawingCache);
        } catch (Throwable ignored) {
        }
    }
    view.setDrawingCacheEnabled(false);
    if (screenshotBitmap == null) {
        return null;
    }
    File screenshotFile = new File(Environment.getExternalStorageDirectory().toString(),
            "fwPlayerScreenshot.tmp.jpg");
    if (screenshotFile.exists()) {
        screenshotFile.delete();
        try {
            screenshotFile.createNewFile();
        } catch (IOException ignore) {
        }
    }
    try {
        FileOutputStream fos = new FileOutputStream(screenshotFile);
        screenshotBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (Throwable t) {
        screenshotFile.delete();
        screenshotFile = null;
    }
    return screenshotFile;
}

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

/**
 * http://stackoverflow.com/questions/12742343/android-get-screenshot-of-all-listview-items
 *///from w w w  .  jav a  2s.com
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:Main.java

public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX, float translateY,
        int downSampling, String color) {
    float scale = 1f / downSampling;
    int bmpWidth = (int) (width * scale - translateX / downSampling);
    int bmpHeight = (int) (height * scale - translateY / downSampling);
    Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    canvas.translate(-translateX / downSampling, -translateY / downSampling);
    if (downSampling > 1) {
        canvas.scale(scale, scale);/*w  w w  .  j a  v a  2 s .  c om*/
    }
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);
    view.buildDrawingCache();
    Bitmap cache = view.getDrawingCache();
    canvas.drawBitmap(cache, 0, 0, paint);
    cache.recycle();
    view.destroyDrawingCache();

    return dest;
}

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 . ja  v a  2  s.co  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();
}