0

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

对我来说最有趣的是,当我在这两种情况下都使用 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());
    }
}
4

1 回答 1

0

我已经设法修复它。我需要对绘制对象的创建做一些小的改动。有工作代码:

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());
    }
}
于 2013-06-23T17:53:00.220 回答