繁体   English   中英

颜色变化的算法错误-Android

[英]Algorithmic error with colour changing - Android

我在应用程序中遇到一些问题。 我正在使用画布从textobject(存储在列表中)中绘制文本,当发生碰撞时,我想将颜色从保存在textobject中的普通文本更改为红色。

对我来说最有趣的事情是,当我在两种情况下都使用Android定义的颜色时,一切正常。 我想这里有一些算法问题,但我找不到。

public class Board extends ViewGroup{

Paint textPaint;

public Board(Context context, AttributeSet attrs) {
//...
textPaint = new Paint();
//...
}

protected void onDraw(Canvas canvas) {
//...
int i = 0;
    for (TextElement element : words) {
        textSize = element.getSize() * scaleMap.get(i);
        textPaint = element.getElementPaint();
            if (inContact == i) {
               textPaint.setColor(Color.RED);
            } else {
               textPaint.setColor(element.getElementPaint().getColor());
               //when there is for example textPaint.setColor(Color.BLACK); 
               // everything is working
            }
    textPaint.setTextSize(textSize);
    canvas.drawText(element.getText(), element.getX(), element.getY(),textPaint);
    i++;
    }
//...
}
}

在整个应用程序生命周期中,此类的对象仅创建一次。

编辑:

public class Board extends ViewGroup{

Paint textPaint;
List<TextElement> words = new ArrayList<TextElement>();

public Board(Context context, AttributeSet attrs) {
//...
textPaint = new Paint();
//...
}

protected void onDraw(Canvas canvas) {
//...
int i = 0;
    for (TextElement element : words) {
        textSize = element.getSize() * scaleMap.get(i);
        textPaint = element.getElementPaint();
            if (inContact == i) {
               textPaint.setColor(Color.RED);
            } else {
               textPaint.setColor(element.getElementPaint().getColor());
               //when there is for example textPaint.setColor(Color.BLACK); 
               // everything is working
            }
    textPaint.setTextSize(textSize);
    canvas.drawText(element.getText(), element.getX(), element.getY(),textPaint);
    i++;
    }
//...
}

public void addText(String newWord) {
    textPaint.setTextAlign(Paint.Align.LEFT);
    textPaint.setTextSize(35);
    textPaint.setColor(Color.BLUE);
    TextElement newText = new TextElement(newWord, 35, textPaint, 10, 35);
    words.add(newText);
    scaleMap.add(words.size() - 1, 1.f);
    collisionMap.add(words.size() - 1, new RectF());
    }
}

我已经设法修复了。 我需要在绘画对象创建中进行一些小的更改。 有工作代码:

public class Board extends ViewGroup{

Paint textPaint;
List<TextElement> words = new ArrayList<TextElement>();

public Board(Context context, AttributeSet attrs) {
//...
textPaint = new Paint();
//...
}

protected void onDraw(Canvas canvas) {
//...
int i = 0;
    for (TextElement element : words) {
        textSize = element.getSize() * scaleMap.get(i);
            if (inContact == i) {
               textPaint.setColor(Color.RED);
            } else {
               textPaint.setColor(element.getElementPaint().getColor());
            }
    textPaint.setTextSize(textSize);
    canvas.drawText(element.getText(), element.getX(), element.getY(),textPaint);
    i++;
    }
//...
}

public void addText(String newWord) {
    Paint newTextPaint = new Paint();
    newTextPaint.setTextAlign(Paint.Align.LEFT);
    newTextPaint.setTextSize(35);
    newTextPaint.setColor(Color.BLUE);
    TextElement newText = new TextElement(newWord, 35, newTextPaint, 10, 35);
    words.add(newText);
    scaleMap.add(words.size() - 1, 1.f);
    collisionMap.add(words.size() - 1, new RectF());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM