Сохранение HorizontalScrollView в изображение в Android Studio
Хочу сохранить представление как картинку, он состоит из множества элементов, в том числе HorizontalScrollView и ScrollView. Я делаю следующее: родитель ScrollView у него дочерний LinearLayout (id/ResultQToPDF) который в сохраняю в картинку. В сохраняемом LinearLayout содержится в том числе и HorizontalScrollView. В итоге картинка получается обрезанной сбоку, то есть часть HorizontalScrollView отсутствует, а по вертикали сохраняет корректно.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/strScrollQ"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/no_press_back"
tools:context="com.polyhydro.prk.OutputResultsFindQ">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="7dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ResultQToPDF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/strQ0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="РЕЗУЛЬТАТЫ"
android:textColor="@color/text_color"
android:textSize="28sp" />
<TextView
android:id="@+id/titleFQ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ОПРЕДЕЛЕНИЯ ПРОПУСКНОЙ СПОСОБНОСТИ"
android:textColor="@color/text_color"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="@+id/strQ2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="8"
android:gravity="start"
android:text="Серия трубопровода"
android:textColor="@color/text_color"
android:textSize="18sp" />
<TextView
android:id="@+id/diameterSeries_outputFQ"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="end"
android:textColor="@color/text_color"
android:textSize="18sp" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:scrollbars="horizontal|vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:padding="5dp">
<TableRow>
<TextView
android:id="@+id/strQ9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingTop="3dp"
android:paddingRight="5dp"
android:paddingBottom="3dp"
android:text="Коэффициент\nнаполнения"
android:textColor="@color/text_color"
android:textSize="16sp" />
<TextView
android:id="@+id/strQ10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingHorizontal="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:text="Расход\nжидкости"
android:textColor="@color/text_color"
android:textSize="16sp" />
<TextView
android:id="@+id/strQ11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingHorizontal="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:text="Скорость\nжидкости"
android:textColor="@color/text_color"
android:textSize="16sp" />
<TextView
android:id="@+id/strD10FQAlter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingHorizontal="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:text="Расход\nжидкости"
android:textColor="@color/text_color_alt_no_press"
android:textSize="16sp" />
<TextView
android:id="@+id/strD11FQAlter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingHorizontal="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:text="Скорость\nжидкости"
android:textColor="@color/text_color_alt_no_press"
android:textSize="16sp" />
</TableRow>
Код сохранения
private boolean saveBitMap(View drawView) {
Date dateNow = new Date();
@SuppressLint("SimpleDateFormat") SimpleDateFormat formatForDateNow = new SimpleDateFormat("HH.mm dd.MM.yyyy");
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File dir = new File(root + "/Отчеты PolyHydro");
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = formatForDateNow.format(dateNow);
File file = new File(dir, "Отчет PolyHydro FindD " + fileName + ".jpg");
Bitmap bitmap = getBitmapFromView(drawView);
try {
FileOutputStream oStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, oStream);
oStream.flush();
oStream.close();
galleryAddPic(file);
/* openImage(pictureFile);*/
} catch (IOException e) {
Toast toast = Toast.makeText(this, "Ошибка сохранения: что-то пошло не так", Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
return false;
}
return true;
}
private Bitmap getBitmapFromView(View view) {
//Запоминаем старые значения цветов, чтобы присвоить их обратно после экспорта
Resources resources = getResources();
int oldTextColor = resources.getColor(R.color.text_color);
int oldBackgroundColor = resources.getColor(R.color.no_press_back);
for (TextView textView : textList) textView.setTextColor(Color.BLACK);
if (PressFindHend.isAltMaterialNeeded) {
for (TextView textView : textListAlter) textView.setTextColor(Color.BLACK);
}
//-------------------------------------------------------------
findViewById(R.id.strScrollD).setBackgroundColor(Color.WHITE);
//-------------------------------------------------------------
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
for (TextView textView : textList) textView.setTextColor(oldTextColor);
if (PressFindHend.isAltMaterialNeeded) {
for (TextView textView : textListAlter)
textView.setTextColor(getResources().getColor(R.color.text_color_alt_no_press));
}
//-------------------------------------------------------------------
findViewById(R.id.strScrollD).setBackgroundColor(oldBackgroundColor);
//-------------------------------------------------------------------
return returnedBitmap;
}
