裁剪位图从屏幕底部到屏幕顶部150dp

时间:2017-07-25 11:21:18

标签: android bitmap

我使用屏幕截图,并希望以编程方式 150dp从屏幕底部裁剪位图。 (从屏幕底部删除位图150dp)

怎么做?

这是图片说明:http://imgur.com/TP2ouVp

被修改即可。拍摄屏幕截图的完整代码:

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

    try {

        String folder_main = "APP_FOLDER";
        File f = new File(Environment.getExternalStorageDirectory(), folder_main);
        if (!f.exists()) {
            f.mkdirs();
        }

        // image naming and path  to include sd card  appending name you choose for file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/APP_FOLDER/" + now + ".jpg";

        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);

        Bitmap source = v1.getDrawingCache();
        int x = 0;
        int y = v1.getHeight() ;
        int width = source.getWidth() - x;
        int height = source.getHeight() - y;
        Bitmap bitmap =  Bitmap.createBitmap(source, x, y, width, height);

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

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

我非常困惑。 感谢

1 个答案:

答案 0 :(得分:1)

试试此代码

调用此方法,传入您想要屏幕截图的最外层ViewGroup:

public Bitmap screenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
            150, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

您还可以查看此answers