Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在画布上无限绘制_Android_Android Layout - Fatal编程技术网

Android 在画布上无限绘制

Android 在画布上无限绘制,android,android-layout,Android,Android Layout,我想让仪表板字符串向左滑动,直到我们可以看到单词的其余部分。在此之后,字符串的位置将被重置,过程将重新开始 将其视为文本视图中的平移动画,在文本视图中,它的位置每x秒向左移动一些像素 我的onDraw方法中有这段代码,这不是这里的唯一代码。我有绘制图标的代码,例如: canvas.drawText( item.title, item.rect.centerX() + itemIconSize / 2 + itemIconMargin, item.rect.cente

我想让仪表板字符串向左滑动,直到我们可以看到单词的其余部分。在此之后,字符串的位置将被重置,过程将重新开始

将其视为文本视图中的平移动画,在文本视图中,它的位置每x秒向左移动一些像素

我的onDraw方法中有这段代码,这不是这里的唯一代码。我有绘制图标的代码,例如:

canvas.drawText(
     item.title,
     item.rect.centerX() + itemIconSize / 2 + itemIconMargin,
     item.rect.centerY() - textHeight, paintText
)
问题是,我希望这段代码可以无限执行,但总是减少x轴上的某个值I,这样我就可以得到向左滑动的效果

我怎样才能做到这一点?我不能只是把代码放在一个whiletrue循环中,因为它会阻塞主UI,而不会绘制任何内容

顺便说一下,我们检查字符串是否大于必须写入的空间的方法是在onSizeChanged函数中,我们有一个标志变量shorten,它告诉我们是否需要缩短字符串:

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        var lastX = barSideMargins
        itemWidth = (width - (barSideMargins * 2)) / items.size

        // reverse items layout order if layout direction is RTL
        val itemsToLayout = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
            && layoutDirection == LAYOUT_DIRECTION_RTL
        ) items.reversed() else items

        for (item in itemsToLayout) {
            // Prevent text overflow by shortening the item title
            var shorted = false
            while (paintText.measureText(item.title) > itemWidth - itemIconSize - itemIconMargin - (itemPadding * 2)) {
                item.title = item.title.dropLast(1)
                shorted = true
            }

            // Add ellipsis character to item text if it is shorted
            if (shorted) {
                item.title = item.title.dropLast(1)
                item.title += context.getString(R.string.ellipsis)
            }

            item.rect = RectF(lastX, 0f, itemWidth + lastX, height.toFloat())
            lastX += itemWidth
        }

        // Set initial active item
        applyItemActiveIndex()
    }
我不能一直调用invalidate,因为这样所有画布都会被重新绘制,我只想重新绘制那段代码 更新1 因此,我目前在onDraw上看到的是:

对于drawText函数,它的值为animator callback,我有:

private fun drawText() {
        animator.duration = 500
        animator.addUpdateListener {
            value = animator.animatedValue as Float
            invalidate()
        }
        animator.start()
    }
目前,在这里,我不担心图标上的文字


问题是,我似乎无法使文本在x轴向左平移。即使使用该代码,字符串也不会设置动画。现在它应该向左平移了,对吗?value和animator都是类字段。

Android textview已经有了一些属性,可以帮助您准确地实现所需的功能。它叫帐篷

但是,如果您想自己从头开始实现这个逻辑,有多种方法可以使用canvas实现。翻译画布本身,应用图层和剪辑逻辑或翻译drawText方法。实际的实现取决于您,但主要思想是为画布绘制调用引入额外的变量,并使用动画师对其进行操作

我不能一直调用invalidate,因为这样所有画布都会被重新绘制,我只想重新绘制那段代码


这是在使用Animator更改值后要执行的操作。您发布的视图示例不会导致无效的性能问题。但如果您认为会,invalidate有一个重载类型,它将一个区域作为参数,并且只在画布中使该区域无效。

那么您建议我怎么做?在我的drawCanvas方法中创建ValueAnimator?像这样的?val animator=ValueAnimator.ofInt10,100 animator.addUpdateListener{val value:Int=animator.animatedValue as Int canvas.drawText item.title,item.rect.centerX+itemIconSize/2+itemIconMargin-value,item.rect.centerY-textHeight,paintText invalidate}animator.startYes,您的思路是正确的。但是我认为使用此新函数可以在图标上绘制文本。因此,只需使用画布调用,直到它适合您。请查看画布文档。您可能需要在此处使用剪辑,以确保:valueAnimator和新变量“value”应该是类字段。您可以吗检查我更新的问题?我想我做了你说的?但是画布不会动画…不要在onDraw中启动animator。在其他地方调用该函数。也可以为animator设置重复模式,因为你需要。
private fun drawText() {
        animator.duration = 500
        animator.addUpdateListener {
            value = animator.animatedValue as Float
            invalidate()
        }
        animator.start()
    }