问题
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;
}
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
is used to paint on photoImageView
.
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 :
The image doubles on cropping too much : https://drive.google.com/open?id=0B4jK5QX65b0ZSHdMTWIzZXluZXc
Before zooming in : https://drive.google.com/open?id=0B4jK5QX65b0ZeUk3NUlGUHRSQ0E
This is how a normal picture appears in crop activity : https://drive.google.com/open?id=0B4jK5QX65b0ZNk9oSG1ZaGxYOVk
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.
I'll be happy to provide more info, any help is much appreciated... I am relatively new to android development :)
EDIT
This video might give more perspective to the problem https://drive.google.com/open?id=0B4jK5QX65b0ZSUVFZDlQeTc4QnM
回答1:
public static Bitmap shot(View view) {
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
return bitmap;
}
回答2:
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();
}
You can call this method like YourUtilsClass.captureScreenShotAndSave(mContext, mViewToCapture);
来源:https://stackoverflow.com/questions/41356494/how-to-get-bitmap-of-a-view