简体   繁体   中英

Cropping Image view

i M passing a bitmap from one activity to other, after taking the screen shot

                Bitmap bitmap;
                bitmap = takeScreenshot();
            try {
                //Write file
                String filename = "bitmap.png";
                FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                //Cleanup
                stream.close();
                bitmap.recycle();

                //Pop intent
                Intent in1 = new Intent(this, FinalImageShare.class);
                in1.putExtra("image", filename);
                startActivity(in1);
            } catch (Exception e) {
                e.printStackTrace();
            }

here i am getting the image in other activity , the problem is that the tool bar height is also coming (im hiding the toool bar by setVisibility, )i want to crop the image so that toolbar height wont come.TIA

     imageView=(ImageView)findViewById(R.id.imageView);

    String filename = getIntent().getStringExtra("image");
    try {
        FileInputStream is = this.openFileInput(filename);
        bmp = BitmapFactory.decodeStream(is);

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

You can use createBitmap method, like so:

resizedbitmap = Bitmap.createBitmap(bitmap, 0, 0, yourwidth, yourheight);

Where bitmap is the bitmap you create, and resizedbitmap is the cropped one.

createBitmap() method takes as parameter in this case: bitmap, start X, start Y, width and height.

You can use those two methods to get your width and height:

bitmap.getWidth(), bitmap.getHeight()

Check also this link to learn about the createBitmap method:

Developer site

Or you can use the drawing cache property for this, like this :

View main = findViewById(R.id.view);
Bitmap screenshot;
main.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(main.getDrawingCache());
main.setDrawingCacheEnabled(false);

Or similar to the last one, ctx.getWindow().getDecorView() View to get full screen bitmap cache:

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

Bitmap bmap = view.getDrawingCache();    

int contentViewTop = ctx.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop(); /* skip status bar in screenshot */
Storage.shareBitmapInfo = Bitmap.createBitmap(bmap, 0, contentViewTop, bmap.getWidth(), bmap.getHeight() - contentViewTop, null, true);

view.setDrawingCacheEnabled(false);

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM