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.google.sample.cast.refplayer.chatting.MainActivity.java

public void takeScreenshot(View view) {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {//from w  w  w.ja v  a  2  s.com
        // image naming and path  to include sd card  appending name you choose for file
        //String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
        String mPath = Environment.getExternalStorageDirectory().toString() + "/test3.jpg";

        // create bitmap screen capture
        //View v1 = getWindow().getDecorView().getRootView();
        View v1 = (RelativeLayout) findViewById(R.id.area_Screenshot);
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        //uploadImage("" + now);
        //uploadImage();

        openScreenshot(imageFile);

    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
}

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.");
    }//w  w w  .j a va 2s.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:com.harmazing.aixiumama.activity.ActivityGallery.java

private Bitmap GetandSaveCurrentImage() {
    //Bitmap//from   w w w . j a va 2 s . c  o m
    WindowManager windowManager = getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    int w = display.getWidth();
    int h = display.getHeight();
    Bitmap Bmp = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
    //??
    View decorview = this.getWindow().getDecorView();
    //        decorview.setDrawingCacheEnabled(true);
    //        decorview.buildDrawingCache();
    //        Bmp = decorview.getDrawingCache();
    Canvas c = new Canvas(Bmp);
    decorview.draw(c);
    // 
    Bmp = Bitmap.createBitmap(Bmp, 0, BitmapUtil.dip2px(getApplicationContext(), 50) + getTopViewHeight(), w,
            CuteApplication.getScreenHW(getApplicationContext())[0]
                    - BitmapUtil.dip2px(getApplicationContext(), 10));

    decorview.destroyDrawingCache();
    decorview.setDrawingCacheEnabled(false);
    return Bmp;
}

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);
    view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
    view.buildDrawingCache(true);/*ww w.  j a va  2  s  .co m*/

    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:edu.mum.ml.group7.guessasketch.android.EasyPaint.java

/**
 * This takes the screenshot of the whole screen. Is this a good thing?
 *///from w  w w  . j  a v  a 2 s  . com
private File sendScreenshot(boolean showToast, ApiCallType callType, String label) {
    saveButton.setVisibility(View.INVISIBLE);
    loader.setVisibility(View.VISIBLE);

    View v = findViewById(R.id.CanvasId);
    v.setDrawingCacheEnabled(true);
    Bitmap cachedBitmap = v.getDrawingCache();
    Bitmap copyBitmap = toGrayscale(cachedBitmap.copy(Bitmap.Config.RGB_565, true));
    v.destroyDrawingCache();
    FileOutputStream output = null;
    File file = null;
    try {
        File path = Places.getScreenshotFolder();
        Calendar cal = Calendar.getInstance();

        file = new File(path,

                cal.get(Calendar.YEAR) + "_" + (1 + cal.get(Calendar.MONTH)) + "_"
                        + cal.get(Calendar.DAY_OF_MONTH) + "_" + cal.get(Calendar.HOUR_OF_DAY) + "_"
                        + cal.get(Calendar.MINUTE) + "_" + cal.get(Calendar.SECOND) + ".jpg");
        output = new FileOutputStream(file);
        copyBitmap.compress(CompressFormat.JPEG, 60, output);
        sendPost(file, callType, label);

    } catch (FileNotFoundException e) {
        file = null;
        e.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    if (file != null) {
        if (showToast)
            Toast.makeText(getApplicationContext(), String
                    .format(getResources().getString(R.string.saved_your_location_to), file.getAbsolutePath()),
                    Toast.LENGTH_LONG).show();
        /*
        // sending a broadcast to the media scanner so it will scan the new
        // screenshot.
        Intent requestScan = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        requestScan.setData(Uri.fromFile(file));
        sendBroadcast(requestScan); */

        return file;
    } else {
        return null;
    }
}

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);
    view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
    view.buildDrawingCache(true);//from  ww  w  .ja va 2 s  . c o  m

    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:com.imaginamos.taxisya.taxista.activities.RegisterDriverActivity.java

public void storeImage(ImageView imageView) {
    Bitmap bm;//  ww w. j  a  v  a  2s.co m

    View v = imageView;
    v.setDrawingCacheEnabled(true);
    bm = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);

    String fileName = "image.png";
    File file = new File(fileName);

    try {

        FileOutputStream fOut = openFileOutput(fileName, MODE_PRIVATE);
        bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:info.tellmetime.TellmetimeActivity.java

@Override
public boolean onMenuItemClick(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case R.id.action_mode:
        if (!mSettings.contains(BACKGROUND_MODE_TOGGLED))
            showToast(R.string.info_toggle_mode);

        toggleMode();/*from  w w w  .  j  a  v a  2 s. com*/

        mHider.hide();
        return true;
    case R.id.action_share_screenshot:
        mHider.hideNow();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    View rootView = getWindow().getDecorView().getRootView();
                    rootView.setDrawingCacheEnabled(true);
                    Bitmap screenshot = Bitmap.createBitmap(rootView.getDrawingCache());
                    rootView.setDrawingCacheEnabled(false);

                    showToast(R.string.info_saving_screenshot);

                    ContentValues values = new ContentValues();
                    values.put(MediaStore.Images.Media.TITLE, getString(R.string.app_name));
                    values.put(MediaStore.Images.Media.DISPLAY_NAME, getString(R.string.app_name) + ".jpg");
                    values.put(MediaStore.Images.Media.MIME_TYPE, "image/*");
                    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                    OutputStream outputStream;
                    outputStream = getContentResolver().openOutputStream(uri);
                    screenshot.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                    outputStream.close();
                    screenshot.recycle();

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("image/jpeg");
                    intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
                    String shareText = getString(R.string.app_description)
                            + " Get it at http://tellmetime.info";
                    intent.putExtra(Intent.EXTRA_TEXT, shareText);
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(intent, "Share via"));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        return true;
    case R.id.action_share_app:
        try {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
            String shareText = getString(R.string.app_description) + " Get it at http://tellmetime.info";
            intent.putExtra(Intent.EXTRA_TEXT, shareText);
            startActivity(Intent.createChooser(intent, "Share via"));
        } catch (Exception ignored) {
        }

        return true;
    case R.id.action_rate:
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=info.tellmetime"));
        startActivity(intent);

        return true;
    default:
        return false;
    }
}

From source file:com.android.launcher3.Utilities.java

private static Bitmap takeShot(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();/*from w  w w .j a va  2  s. c  om*/
    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;

    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay().getHeight();

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();
    return b;
}

From source file:tv.piratemedia.lightcontroler.controller.java

private File getScreen() {
    Long tsLong = System.currentTimeMillis() / 1000;
    String ts = tsLong.toString();
    String mPath = Environment.getExternalStorageDirectory().toString() + "/bug_report_" + ts + ".jpg";

    // create bitmap screen capture
    Bitmap bitmap;//from  ww  w.  j a va2 s  . c o  m
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    OutputStream fout = null;
    File imageFile = new File(mPath);

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();
        fout.close();
        return imageFile;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}