我已经尝试了很多方法。我能够截屏整个屏幕,但我的要求是采取整个HorizontalScrollView截图并将其保存为图像文件。下面是我使用的示例XML布局:捕获HorizontalScrollView的屏幕截图,包括离屏元素
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hsv" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:id="@+id/ll">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="click"
android:id="@+id/b"
android:text="capture"
android:layout_margin="20dp"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/img1"
android:src="@drawable/plug1"
android:layout_margin="20dp"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="200dp"
android:id="@+id/img2"
android:src="@drawable/plug3"
android:layout_margin="20dp"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
活动代码:
Button b;
ImageView img1, img2;
LinearLayout ll; View v;
HorizontalScrollView s;
int h,w;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button)findViewById(R.id.b);
img1 = (ImageView)findViewById(R.id.img1);
img2 = (ImageView)findViewById(R.id.img2);
ll = (LinearLayout)findViewById(R.id.ll);
s = (HorizontalScrollView) findViewById(R.id.hsv);
s.setDrawingCacheEnabled(true);
}
//Fired onClick of the button
public void click(View v) {
shoot();
}
// To capture layout, create a bitmap and replace an image with the resulting bitmap.
public void shoot() {
s.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int totalHeight = s.getMeasuredHeight();
int totalWidth = s.getMeasuredWidth();
s.layout(0, 0, totalHeight, totalWidth);
s.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(s.getDrawingCache());
s.setDrawingCacheEnabled(false);
img1.setImageBitmap(b);
}
,如果我不使用MeasureSpec我没有得到正确的布局的高度和宽度。
OnClick的按钮我只得到半按钮的图像。我认为这是因为我没有使用ViewTreeObserver。但是当我使用ViewTreeObserver时,我没有得到结果。此外,当我单击按钮两次时,应用程序崩溃 - Bitmap中的NullPointerException。
我已经尝试了几乎所有可用的StackOverFlow。请帮助我找到一种方法来做到这一点。它甚至有可能吗?
谢谢。你可以建议我一些东西,我可以放大scrollview的全部内容以适应屏幕,然后截取屏幕截图。如果可能的话,任何答案都会有很大的帮助。 –
找到了一个方法。请参阅我的回答。 –