Я пробовал много методов. Я могу сделать снимок экрана всего экрана, НО мое требование - сделать снимок экрана всего HorizontalScrollView и сохранить его как файл изображения. Вот пример макета XML, который я использую:
<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.
При нажатии кнопки я получаю изображение только половины кнопки. Я думаю, что это происходит, поскольку я не использую ViewTreeObserver. Но когда я использую ViewTreeObserver, я не получаю результата. Кроме того, приложение вылетает, когда я дважды нажимаю кнопку - NullPointerException в Bitmap.
Я пробовал почти все, что доступно на StackOverFlow. Пожалуйста, помогите мне найти способ сделать это. Это вообще возможно?