带有自定义视图的 ListView 屏幕截图越界
ListView Screenshot with custom view get off bounds
我正在尝试以编程方式截屏 ListView- 可见行和不可见行。我的 ListView 包含带有我制作的自定义图表的行 - 作为 subclass 到 View
和一个 header 标题 - TextView
.
显示时一切正常,但是当我生成屏幕截图时,所有图表都越界了——所有底部部分都消失了,看起来它扩展到行的整个高度而没有注意到其他组件(如标题 TextView
)。
我已将测试 Project 上传到 Github
我的问题 - 如何在生成屏幕截图时保留图表 in-bounds?
这是正常使用该应用程序时的样子-
这是它在生成的屏幕截图中的样子-
这是生成我的屏幕截图的代码-
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
}
我的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-
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)
}
在 GraphContainer 中更改此行
canvasWidth = canvas.width.toFloat()
canvasHeight = canvas.height.toFloat()
到此。
canvasWidth = width.toFloat()
canvasHeight = height.toFloat()
并且在graph_item_list.xml
android:layout_height="300dp"
到这个
android:layout_height="wrap_content"
我向您发送了拉取请求。此致
我正在尝试以编程方式截屏 ListView- 可见行和不可见行。我的 ListView 包含带有我制作的自定义图表的行 - 作为 subclass 到 View
和一个 header 标题 - TextView
.
显示时一切正常,但是当我生成屏幕截图时,所有图表都越界了——所有底部部分都消失了,看起来它扩展到行的整个高度而没有注意到其他组件(如标题 TextView
)。
我已将测试 Project 上传到 Github
我的问题 - 如何在生成屏幕截图时保留图表 in-bounds?
这是正常使用该应用程序时的样子-
这是它在生成的屏幕截图中的样子-
这是生成我的屏幕截图的代码-
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
}
我的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-
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)
}
在 GraphContainer 中更改此行
canvasWidth = canvas.width.toFloat()
canvasHeight = canvas.height.toFloat()
到此。
canvasWidth = width.toFloat()
canvasHeight = height.toFloat()
并且在graph_item_list.xml
android:layout_height="300dp"
到这个
android:layout_height="wrap_content"
我向您发送了拉取请求。此致