首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >截图怎么裁剪?

截图怎么裁剪?
EN

Stack Overflow用户
提问于 2018-09-05 09:00:55
回答 1查看 210关注 0票数 0

你好,我有一个代码来采取截图,然后分享它和它的工作很好,但我想从顶部和底部裁剪这个截图。我希望能对我有所帮助,因为我在编程方面是新手。下面是我使用的代码:

代码语言:javascript
运行
复制
share.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Bitmap bitmap = takeScreenshot();
        saveBitmap(bitmap);
        shareIt();
    }


    public Bitmap takeScreenshot() {
        View rootView = findViewById(android.R.id.content).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
    }

    private void saveBitmap(Bitmap bitmap) {
        imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); ////File imagePath
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }

    private void shareIt() {
        Uri myUri = Uri.fromFile(imagePath);
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/*");
        sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, myUri);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
});


    displayResults();

}
EN

回答 1

Stack Overflow用户

发布于 2018-09-05 09:38:01

如果你想使用库,你可以实现这个library。并遵循下面的代码

来自ArthurHub/Android-Image-Cropper

AndroidManifest.xml

代码语言:javascript
运行
复制
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if default theme has no action bar) -->

启动CropImageActivity的

代码语言:javascript
运行
复制
// start picker to get image for cropping and then use the image in cropping 
activity
CropImage.activity()
         .setGuidelines(CropImageView.Guidelines.ON)
         .start(this);

// start cropping activity for pre-acquired image saved on the device
CropImage.activity(imageUri)
         .start(this);

// for fragment (DO NOT use `getActivity()`)
CropImage.activity()
         .start(getContext(), this);

覆盖onActivityResult

代码语言:javascript
运行
复制
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
           Uri resultUri = result.getUri();
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
          Exception error = result.getError();
        }
      }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52175714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档