颜色变化的算法错误 - Android

时间:2013-06-21 19:06:58

标签: java android

我在应用程序中遇到了一些问题。我正在使用canvas从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());
    }
}

1 个答案:

答案 0 :(得分: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());
    }
}