【问题标题】:How to get bitmap of a view? [duplicate]如何获取视图的位图? [复制]
【发布时间】:2017-05-12 09:48:47
【问题描述】:

好的,所以我会尽力解释我的问题。 我已使用此代码获取 FrameLayout 的“屏幕截图”

/**
 * Function that takes a screenshot of the view passed and returns a bitmap for it.
 *
 * @param view {@link View}
 * @return screenshot of the view passed
 */
public Bitmap screenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
            view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

这是我的框架布局

<FrameLayout
    android:id="@+id/frame_layout_picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/photoImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter" />

    <com.my.android.util.DrawCustomView
        android:id="@+id/draw_custom_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

DrawCustomView 用于在photoImageView 上绘画。

现在我也有不同的活动来裁剪图像。所以,我将这个位图(使用screenshot(view))保存在一个文件中,并将文件路径发送到这个裁剪活动。在我的裁剪活动中,我使用https://github.com/ArthurHub/Android-Image-Cropper 库的CropImageView 来裁剪图像。 当我尝试裁剪发送到裁剪活动的绘制照片时,我得到了一个奇怪的裁剪,如下所示:

图像因裁剪过多而加倍: https://drive.google.com/open?id=0B4jK5QX65b0ZSHdMTWIzZXluZXc

放大前: https://drive.google.com/open?id=0B4jK5QX65b0ZeUk3NUlGUHRSQ0E

这是正常图片在裁剪活动中的显示方式: https://drive.google.com/open?id=0B4jK5QX65b0ZNk9oSG1ZaGxYOVk

我还注意到,在正常情况下,裁剪的边界仅限于图像大小,但使用 screenshot() 进行编辑时,边界会占用屏幕的大小。

我事先测试了我的裁剪活动,它适用于从相机发送的图像。 我还尝试发送一张不是使用截图功能获得的图片,并且在裁剪活动中效果很好。

我很乐意提供更多信息,非常感谢任何帮助...我对 android 开发比较陌生 :)

编辑

此视频可能会更深入地了解问题 https://drive.google.com/open?id=0B4jK5QX65b0ZSUVFZDlQeTc4QnM

【问题讨论】:

    标签: android bitmap android-canvas android-graphics android-crop


    【解决方案1】:
    public static Bitmap shot(View view) {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();
        view.setDrawingCacheEnabled(false);
        return bitmap;
    } 
    

    【讨论】:

    【解决方案2】:

    你可以试试这个方法来截取你的视图。

     /**
      * this method capture the image from provided view and save it to application internal directory.
      *
      * @param view to capture image.
      * @return path of image saved in the phone.
      */
    public static String captureScreenShotAndSave(Context mContext, View view) {
        view.setBackgroundColor(Color.WHITE);
        String mPath = mContext.getApplicationContext().getExternalCacheDir() + "/" + "IMG_" + DateTimeUtils.getCurrentTimeStamp() + "_" + SHARE_IMAGE_NAME;
    
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        view.draw(c);
        c.drawBitmap(bitmap, 0, 0, null);
    
        OutputStream fout = null;
        File imageFile = new File(mPath);
        try {
            fout = new FileOutputStream(imageFile);
    
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fout);//set image quality and formate as you required.
            fout.flush();
            fout.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageFile.getAbsolutePath();
    }
    

    你可以像YourUtilsClass.captureScreenShotAndSave(mContext, mViewToCapture);这样调用这个方法

    【讨论】:

    • Android studio 无法解析 AppDelgate 和 DateTimeUtils。
    • 检查我的最新编辑。
    • 不幸的是,同样的问题仍然存在:drive.google.com/open?id=0B4jK5QX65b0ZSUVFZDlQeTc4QnM
    猜你喜欢
    • 1970-01-01
    • 2012-08-03
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多