如何获取视图的位图?

时间:2016-12-28 06:25:33

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

好的,所以我会尽力解释我的问题。 我已使用此代码获取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

<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

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此方法捕获视图的屏幕截图。

 /**
  * 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);

答案 1 :(得分:0)

public static Bitmap shot(View view) {
    view.setDrawingCacheEnabled(true);
    Bitmap bitmap = view.getDrawingCache();
    view.setDrawingCacheEnabled(false);
    return bitmap;
}