【问题标题】:How to change color of Rects instantly when touched?触摸时如何立即改变矩形的颜色?
【发布时间】:2020-03-23 09:15:40
【问题描述】:

我正在开发一个使用 ml kit 文本识别库的应用程序;该应用程序从图像中读取文本,并在每个单词周围加上Rect。 现在我希望这些Rects 在用户点击其中一个或在某些单词上方滑动时改变颜色。 所以,我能够正确处理触摸事件,但 我不能改变触摸的颜色 Rect!

我应该在触摸的矩形上方绘制新的彩色矩形吗?或者我可以为现有的矩形着色(我认为我不能)?

类: TextGraphicGraphicOverlay//这是绘制矩形的地方

我也试过this solution,所以我在TextGraphic类中输入了这个方法:

public void changeColor(boolean isHighlighted) {
    if(isHighlighted) {
        rectPaint.setColor(COLOR_HIGHLIGHTED);
        rectPaint.setAlpha(400);//set opacity of rect
    }else{
        rectPaint.setColor(COLOR_DEFAULT);
        rectPaint.setAlpha(400);//set opacity of rect
    }
    postInvalidate();
}

并在用户触摸文本时调用它,但问题是所有Rects 的颜色都会改变,而且它们在运行时不会改变!

我的 ActivityClass 中的一个 sn-p,我在其中使用了一些回调方法来传递信息。

ArrayList<Rect> rects = new ArrayList<>();

@Override
public void onAdd(FirebaseVisionText.Element element, Rect elementRect, String wordText) {
   GraphicOverlay.Graphic textGraphic = new TextGraphic(mGraphicOverlay, element);
   mTextGraphic = new TextGraphic(mGraphicOverlay, element);
   mGraphicOverlay.add(textGraphic);


   rects.add(elementRect);
}

我处理触摸事件的 ActivityClass 中的一个 sn-p:

@Override
public boolean onDown(MotionEvent event) {
   helper.dismissKeyboard();
   touchX = Math.round(event.getX());
   touchY = Math.round(event.getY());
   for(int x=0; x< rects.size();x++) {
       if (rects.get(x).contains(touchX, touchY)) {
           // line has been clicked
           mTextGraphic.changeColor(true);

           return true;
       }
   }
   return true;
}

【问题讨论】:

  • 可以显示更多代码,您如何以及在何处处理触摸事件。
  • 好的,我更新了帖子。谢谢:D
  • 实际上颜色变化意味着您实际上将用不同的颜色重绘相同的Rect。你在哪里画你的RectonDraw()?你能展示一下吗?我假设您将这个color 变量传递给onDraw() 中的Paint 对象。我还看到你使用postInvalidate() 而不仅仅是invalidate() 你是从不同的线程调用它吗?
  • 您可以在我提到的 TextGraphic 和 GraphicOverlay 类中检查这一点。这里又是链接:github.com/firebase/quickstart-android/blob/master/mlkit/app/…github.com/firebase/quickstart-android/blob/master/mlkit/app/…我很抱歉,但我不太习惯使用/绘制矩形,所以我有点'迷路了
  • “所有 Rect 颜色都已更改并且它们在运行时不会更改”是什么意思。它有点模糊。当您说它们在运行时不会更改时,我假设您的矩形在应用程序运行时单击时根本不会改变颜色。但是您也说所有矩形颜色都已更改。你能澄清一下吗?

标签: android highlight firebase-mlkit text-recognition android-rect


【解决方案1】:

您正在使用mTextGraphic 变量更改颜色。如果您仔细查看 onAdd() 方法,您会发现您正在为 mTextGraphic 分配一个新对象,这与绘制到屏幕上的对象无关,因为只有您添加到 GraphicOverlay 的对象使用 @ 列出987654325@ 将被绘制到屏幕上。

所以你需要调用changeColor(),而不是mTextGraphic,而是在GraphicOverlay内的列表中已经存在的相应对象上

由于GraphicOverlay 中的列表是私有的,因此您无法在onDown() 方法中对其进行操作。您需要编写一个公共方法来为您完成这项工作。

GraphicOverlay类中编写如下方法

public TextGraphic getGraphicElementAtIndex(int index) {
    return (TextGraphic)graphics.get(index)
}

现在像这样在onDown()方法的if条件中使用这个方法

if (rects.get(x).contains(touchX, touchY)) {
    // line has been clicked
    Log.d("PreviewActivity", "changeColor() is called");
    mGraphicOverlay.getGraphicElementAtIndex(x).changeColor();
    touchedText = texts.get(x);
    return true;
}

希望这会有所帮助。

旁注: 现在即使在此之后,如果由于某种原因,rects 列表和 graphics 列表中的对象排序(在 GraphicOverlay 中)改变然后你会看到当你点击一个矩形时,其他一些矩形会改变颜色。

【讨论】:

  • 非常感谢赛义德大师!你的解决方案很完美。是的,我能够通过每次设置读取新图像时重置矩形数组列表来解决您在旁注中提到的问题:)
【解决方案2】:

也许你不应该通过编码而是通过 ColorStateList 来做到这一点 Android Developer: colorstatelist

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 2015-04-21
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多