Я пытаюсь сделать программный снимок экрана ListView - как видимых, так и невидимых строк.Мой ListView содержит строки с пользовательским графиком, который я сделал - в качестве подкласса к View
и заголовок заголовка - TextView
.
Все хорошо работает при отображении, но когда я генерирую снимок экрана все графики выходят за границы - вся нижняя часть исчезла , похоже, она расширяетсяна всю высоту строки, не обращая внимания на другие компоненты (например, название TextView
).
Я загрузил тест Project в Github
Мой вопрос-Как я могу сохранить свои графики при создании этого скриншота?
Вот как это выглядит при обычном использовании приложения - 
Вот как это выглядитсгенерированный скриншот - 
Вот код, который генерирует мой скриншот -
fun getWholeListViewItemsToBitmap(list: ListView): Bitmap {
val adapter = list.adapter
val itemsCount = adapter.getCount()
var allItemsHeight = 0
val bmps = ArrayList<Bitmap>()
for (i in 0 until itemsCount) {
val childView = adapter.getView(i, null, list)
childView.measure(
View.MeasureSpec.makeMeasureSpec(list.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
)
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight())
childView.isDrawingCacheEnabled = true
childView.buildDrawingCache()
childView.drawingCacheBackgroundColor = Color.WHITE
bmps.add(childView.drawingCache)
allItemsHeight += childView.getMeasuredHeight()
}
val bigBitmap = Bitmap.createBitmap(list.getMeasuredWidth(), allItemsHeight, Bitmap.Config.ARGB_8888)
val bigCanvas = Canvas(bigBitmap)
val paint = Paint()
var iHeight = 0f
for (i in bmps.indices) {
var bmp: Bitmap? = bmps[i]
bigCanvas.drawBitmap(bmp, 0f, iHeight, paint)
iHeight += bmp!!.height.toFloat()
bmp.recycle()
bmp = null
}
return bigBitmap
}
My graph_list_item.xml
-
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="300dp">
<com.niv.test.Graphs.GraphContainer
android:id="@+id/graphContainer"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView"/>
<TextView
android:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="@android:color/black"/>
</android.support.constraint.ConstraintLayout>
Мой GraphContainer
класс-
class GraphContainer: View {
/** the color of the rect */
var color = Color.TRANSPARENT
/** graph's title */
var title: String? = null
var titleFontSize: Float = 60f
/** when positive- moves title to right, when negative- moves title to left */
var titleXOffset = 0f
/** when positive- moves title up, when negative- moves title down */
var titleYOffset = 0f
var titleColor = Color.BLACK
private var graphViews: MutableList<View> = mutableListOf()
fun addGraph(graph: View){
graphViews.add(graph)
}
fun addGraphs(graphs: List<View>){
graphViews.addAll(graphs)
}
fun clearGraphs(){
while (!graphViews.isEmpty()){
graphViews.removeAt(0)
}
}
/** the header's height (makes room for the graph's title) */
var headerHeight = 100f // initialized later
/** dedicated frame for header */
val headerFrame: RectF get() = RectF(0f,0f,0f,0f) // initialized later
/** the footer's height */
var footerHeight = 0f
/** dedicated frame for graphs- the space left when excluding the header and footer */
val graphRect: RectF get() = RectF(0f, headerHeight, canvasWidth, canvasHeight - footerHeight)
private var canvasWidth = 0f
private var canvasHeight = 0f
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvasWidth = canvas.width.toFloat()
canvasHeight = canvas.height.toFloat()
canvas.drawColor(color)
for (i in graphViews.indices){
val graphView = graphViews[i]
val graphBitmap = Bitmap.createBitmap(graphRect.width().toInt(),graphRect.height().toInt(), Bitmap.Config.ARGB_8888)
val graphCanvas = Canvas(graphBitmap)
graphView.draw(graphCanvas)
canvas.drawBitmap(graphBitmap, graphRect.left, graphRect.top, Paint())
}
if (title != null){
val paint = Paint()
paint.textSize = titleFontSize
paint.color = titleColor
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD))
val point = PointF(titleXOffset, titleFontSize + titleYOffset)
canvas.drawText(title!!, paint, point)
}
}
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet): super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyle: Int): super(context, attrs, defStyle)
}