Take a Screenshot of a Whole View

Take a screenshot of a whole View

Actually I found the answer:

public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth() , v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}

Take whole Screenshot of a scrollable View

I hope this is work for you.. source here. this is not technically a screenshot code. but this code convert the whole layout view into bitmap

Bitmap bitmap = getBitmapFromView(scrollview, scrollview.getChildAt(0).getHeight(), scrollview.getChildAt(0).getWidth());

//create bitmap from the ScrollView
private Bitmap getBitmapFromView(View view, int height, int width) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return bitmap;
}

How to take screenshot of WHOLE activity page programmatically?

One way is to extract the bitmap of the current screen and save it. Try this:

public void takeScreenshot(){
Bitmap bitmap = getScreenBitmap(); // Get the bitmap
saveTheBitmap(bitmap); // Save it to the external storage device.
}

public Bitmap getScreenBitmap() {
View v= findViewById(android.R.id.content).getRootView();
v.setDrawingCacheEnabled(true);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
return b;
}

Android take screen shot programmatically

What is the simplest way to get screenshot in android using kotlin?

Add desired views in xml layout inflate it and take screenshot of parent layout that is containing your views.

Code for taking screenshoot:

 fun takeScreenshotOfView(view: View, height: Int, width: Int): Bitmap {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val bgDrawable = view.background
if (bgDrawable != null) {
bgDrawable.draw(canvas)
} else {
canvas.drawColor(Color.WHITE)
}
view.draw(canvas)
return bitmap
}


Related Topics



Leave a reply



Submit