【问题标题】:Checking for images on setOnClickListener() in view latency reduction检查 setOnClickListener() 上的图像以减少视图延迟
【发布时间】:2020-04-17 19:15:49
【问题描述】:

如何避免检查印刷机上的大量图像以减少延迟?我想通过不丢失检查功能尽快使TextView1 变为绿色。

TextView1.setOnClickListener {

    if (image_view.drawable.constantState == ContextCompat.getDrawable(
            this,
            R.drawable.cat__1_
        )?.constantState ||
        image_view.drawable.constantState == ContextCompat.getDrawable(
            this,
            R.drawable.cat__2_
        )?.constantState ||
        image_view.drawable.constantState == ContextCompat.getDrawable(
            this,
            R.drawable.cat__3_
        )?.constantState ||
        image_view.drawable.constantState == ContextCompat.getDrawable(
            this,
            R.drawable.cat__4_
        )?.constantState



    ) {
        TextView1.setBackgroundResource(R.color.green);
        Handler().postDelayed({
            TextView1.setBackgroundResource(R.color.white)
        }, 50)
}

已编辑:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
showGreen = false
    TextView1.setOnClickListener {

                    showGreen = isGreenBackgroundShouldAppear()
}
}

fun isGreenBackgroundShouldAppear(): Boolean {
       return image_view.drawable.constantState == ContextCompat.getDrawable(
                this,
                R.drawable.cat__1_
            )?.constantState ||
            image_view.drawable.constantState == ContextCompat.getDrawable(
                this,
                R.drawable.cat__2_
            )?.constantState ||
            image_view.drawable.constantState == ContextCompat.getDrawable(
                this,
                R.drawable.cat__3_
            )?.constantState ||
            image_view.drawable.constantState == ContextCompat.getDrawable(
                this,
                R.drawable.cat__4_
            )?.constantState



        // Do the checking here
        // and set the showGreen variable
    }

【问题讨论】:

  • 您实际上是在处理程序中使用延迟?你们用什么来减少延迟?那里没有异步,只是简单的线性代码将可运行的内容移动到条件流的底部

标签: android kotlin latency


【解决方案1】:

我建议预先计算加载到image_view.drawable 中的可绘制对象的状态,并在点击侦听器中检查状态值以加载必要的资源。

我不确定您在哪里加载此图像,但是,如果这是一个活动,请在活动的onCreate 函数中进行如下预计算。

public boolean showGreen = false;

public void onCreate() {
    showGreen = isGreenBackgroundShouldAppear();
}

public boolean isGreenBackgroundShouldAppear() {
    // Do the checking here
    // and set the showGreen variable
}

然后在你的TextView1onClickListener 中,读取showGreen 的值并自动分配背景。

如果同时更新了图片drawable,则需要确保每次调用isGreenBackgroundShouldAppear函数将正确的值加载到showGreen变量中。

请注意,我刚刚提供了一些 Java 伪代码。我希望这有助于解决您的问题。

更新:你可以试试这样的。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    showGreen = isGreenBackgroundShouldAppear()

    TextView1.setOnClickListener {
         if (showGreen) {
             // Set the green background here
         } else {
             // Set the other background
         }
     }
}

【讨论】:

  • 感谢您的回答,但我仍然不明白如何设置“showGreen”值以及将 TextView1 变为绿色的代码的放置位置。如果我尝试将 showGreen = true 放在 isGreenBackgroundShouldAppear() 中,它会显示错误。你可以看看我编辑的帖子我尝试了什么。
  • 谢谢。我使用线程每隔 100 毫秒定期更新showGreen = isGreenBackgroundShouldAppear()。我希望这是这个案例的最佳解决方案。
  • 我在 LG G4 上试过,但在更差的设备上试过,但 100 毫秒可能还不够。我实际上使用了 8 张图片。
猜你喜欢
  • 2019-12-12
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 2018-04-10
  • 2021-10-22
相关资源
最近更新 更多