Capture Screenshot Of A Horizontalscrollview Including Offscreen Elements
Solution 1:
Found a SIMPLE way to do it.
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"android:layout_gravity="center_vertical"><HorizontalScrollViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"
><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:orientation="horizontal"android:id="@+id/def"
><Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:text="capture"android:onClick="click"/><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/black"android:id="@+id/img"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/a"/></LinearLayout></HorizontalScrollView></LinearLayout>
Acivity Code:
publicclassCaptureBeyondActivityextendsActivity {
/** Called when the activity is first created. */
LinearLayout def;
ImageView img;
HorizontalScrollView hsv;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
def = (LinearLayout)findViewById(R.id.def);
img = (ImageView)findViewById(R.id.img);
}
publicvoidclick(View v) {
Bitmap bitmap;
def.setDrawingCacheEnabled(true);
def.buildDrawingCache(true);
bitmap = Bitmap.createBitmap(def.getWidth(), def.getHeight(),
Config.ARGB_8888);
def.layout(0, 0, def.getLayoutParams().width,
def.getLayoutParams().height);
Canvasc=newCanvas(bitmap);
def.draw(c);
def.setDrawingCacheEnabled(false);
if(bitmap!=null){
img.setImageBitmap(bitmap);
img.invalidate();
}
}}
Here I have placed some elements in a HorizontalScrollView. OnClick of the button a screenshot is taken and the second image is replaced by the screenshot taken. It takes screenshots of the offscreen elements also. You can further enhance the code and save the final bitmap image on SD card.
Solution 2:
it's the second time I see this same question (yes, here in stack overflow) and the simple answer is: There is no simple way to do it!
All the methods to save a screen shot uses the same principle of enabling the draw to the bitmap cache and obtaining this bitmap. But if the views are not on screen, they won't be drawn.
You could create a very complex system that would auto-scroll the Horizontal screen view, taking several screen shots and stitching the different bitmaps together. Or maybe a very complex system that would create an infinite width canvas and ask the layout to draw on it, but it's likely that you would run out of memory on this situation. But as I said, none of those is a simple task.
Post a Comment for "Capture Screenshot Of A Horizontalscrollview Including Offscreen Elements"