如何获取视图的位图? [英] How to get bitmap of a view?

查看:134
本文介绍了如何获取视图的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok, so I'll try my best to explain my problem. I have used this code to get a "screenshot" of my 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

Here is my 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来裁剪图像. 当我尝试裁剪发送给裁剪活动的绘画照片时,我得到了奇怪的裁剪,如下所示:

Now I also have different activity to crop image. So, I saved this bitmap (using screenshot(view)) in a file and send the file path to this crop activity. In my crop activity I am using https://github.com/ArthurHub/Android-Image-Cropper library's CropImageView to crop the image. When I try to crop the painted photo sent to crop activity I get a weird crop, something like this :

如果裁剪过多,图像会加倍: https://drive.google.com/open?id=0B4jK5QX65b0ZSHdMTWIzZXluZXc

The image doubles on cropping too much : https://drive.google.com/open?id=0B4jK5QX65b0ZSHdMTWIzZXluZXc

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

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

This is how a normal picture appears in crop activity : https://drive.google.com/open?id=0B4jK5QX65b0ZNk9oSG1ZaGxYOVk

我还注意到,在正常情况下,裁剪的范围仅限于图像大小,但是当使用screenshot()编辑时,该范围会占据屏幕的大小.

I also notice that the bounds for cropping are restricted to image size in the normal case but when edited using screenshot(), the bounds take the size of the screen.

我已经预先测试了农作物的活动,并且对从相机发送的图像效果很好. 我还尝试发送无法使用屏幕截图功能获取的图片,并且该图片在农作物活动中效果很好.

I have tested my crop activity beforehand and it works fine for images sent from camera. I also tried to send a picture that is not obtained using screenshot function, and it worked fine in crop activity.

我很乐意提供更多信息,非常感谢您的帮助...我对android开发来说还是比较新的东西:)

I'll be happy to provide more info, any help is much appreciated... I am relatively new to android development :)

编辑

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

This video might give more perspective to the problem https://drive.google.com/open?id=0B4jK5QX65b0ZSUVFZDlQeTc4QnM

推荐答案

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

You can try this method to capture a screenshot of your view.

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

这篇关于如何获取视图的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆