具有自定义视图的ListView屏幕截图越界

时间:2018-12-07 14:24:10

标签: java android listview kotlin android-bitmap

我正在尝试以编程方式对ListView进行屏幕截图-可见行和不可见行。我的ListView包含具有自定义图形的行,这些图形是我制作的-作为View的子类和标头标题-TextView

在显示时,所有显示效果都很好,但是当我生成屏幕截图时,所有图都越界了-所有底部都消失了,看起来它扩展为整个行的高度,而不会注意到其他组件(例如标题TextView)。

我已将测试Project上传到Github

我的问题-生成该屏幕截图时如何使图形保持边界?

以下是正常使用该应用程序时的外观- normal screenshot

这是在生成的屏幕截图上的外观- full screenshot

以下是生成我的屏幕截图的代码-

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 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)
}

1 个答案:

答案 0 :(得分:0)

在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"

我向您发送请求请求。问候