how to zoom + crop a image and display the croped image on imageview

前端 未结 2 1778
陌清茗
陌清茗 2020-11-30 13:35

I am making android app in which I have a scenario that I pick an image from gallery, crop it and show it on imageview. Now at the time of cropping I want to zo

2条回答
  •  囚心锁ツ
    2020-11-30 13:55

    I am using cropimage library from Github. It fits your requirement well. This is how I use this library in my project. Add this line to your manifest file:

     
    

    Select image from gallery or camera and call this function:

    public void runCropImage(String path) {
        Intent intent = new Intent(this, CropImage.class);
        intent.putExtra(CropImage.IMAGE_PATH, path);
        intent.putExtra(CropImage.SCALE, true);
        intent.putExtra(CropImage.ASPECT_X, 2);//change ration here via intent
        intent.putExtra(CropImage.ASPECT_Y, 2);
        startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);//final static int 1
    }
    

    And in your onActivityResult:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        // gallery and camera ommitted
        case REQUEST_CODE_CROP_IMAGE:
            String path = data.getStringExtra(CropImage.IMAGE_PATH);
            // if nothing received
            if (path == null) {
                return;
            }
            // cropped bitmap
             Bitmap bitmap = BitmapFactory.decodeFile(path);
            imageView.setImageBitmap(bitmap);
            break;
        default:
            break;
        }
    }
    

提交回复
热议问题