My problem is all the instances keep drawing the text using the same color even when I assigned different colors to different instances. The canvas passed to the object is a canvas made from a bitmap which is used as a frame buffer.
Using java Syntax Highlighting
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Paint.Align;
- import android.graphics.Paint.Style;
- import android.graphics.Rect;
- public class ETFader{
- public static final int VERTICAL_BASELINE = 0;
- public static final int VERTICAL_TOP = 2;
- public static final int VERTICAL_CENTER = 4;
- public static final int VERTICAL_BOTTOM = 8;
- public String text = "";
- public float x = 0;
- public float y = 0;
- public int w = 0;
- public int h = 0;
- public boolean visible = true;
- public boolean alive = true;
- public boolean started = false;
- private float alpha = 255;
- private int vAlign = VERTICAL_CENTER;
- private long frameOffset = 0;
- private long frameStart = 0;
- private long frameDelay = 10;
- private Canvas canvas = null;
- private Paint paint = null;
- private Paint outline = null;
- private int color1 = 0;
- private int color2 = 0;
- private Rect bounds = null;
- public ETFader(Canvas c, String txt, float sx, float sy, float sz, int col1, int col2) {
- canvas = c;
- text = txt;
- x = sx;
- y = sy;
- alpha = 255;
- alive = false;
- started = false;
- frameOffset = 1000;
- color1 = col1;
- color2 = col2;
- paint = new Paint();
- // paint.setTypeface(Fonts.item("black"));
- paint.setTextSize(sz);
- paint.setAntiAlias(true);
- paint.setTextAlign(Align.CENTER);
- paint.setColor(color1);
- outline = new Paint();
- // outline.setTypeface(Fonts.item("black"));
- outline.setTextSize(sz);
- outline.setAntiAlias(true);
- outline.setTextAlign(Align.CENTER);
- outline.setStyle(Style.STROKE);
- outline.setStrokeWidth(3);
- outline.setColor(color2);
- bounds = new Rect();
- paint.getTextBounds(text, 0, text.length(), bounds);
- w = bounds.width();
- h = bounds.height();
- }
- public void show(String txt, float sx, float sy, long start) {
- frameStart = System.currentTimeMillis();
- visible = true;
- text = txt;
- x = sx;
- y = sy;
- alpha = 255;
- alive = true;
- started = false;
- frameOffset = start;
- paint.setAlpha(255);
- outline.setAlpha(255);
- paint.getTextBounds(text, 0, text.length(), bounds);
- w = bounds.width();
- h = bounds.height();
- }
- public void destroy() {
- visible = false;
- alive = false;
- paint = null;
- outline = null;
- canvas = null;
- }
- public void update() {
- if (!visible) return;
- if (!alive) return;
- if (!started){
- if ((System.currentTimeMillis() - frameStart) < frameOffset) return;
- frameStart = System.currentTimeMillis();
- started = true;
- }
- if ((System.currentTimeMillis() - frameStart) < frameDelay) return;
- frameStart = System.currentTimeMillis();
- if (alpha > 1){
- alpha *= 0.5;
- outline.setAlpha((int)alpha);
- paint.setAlpha((int)alpha);
- } else {
- outline.setAlpha(0);
- paint.setAlpha(0);
- alive = false;
- }
- }
- public float getY(float sy){
- if (vAlign == VERTICAL_TOP){
- return sy - bounds.top;
- }
- if (vAlign == VERTICAL_CENTER){
- return (sy - bounds.top) - (this.h / 2);
- }
- if (vAlign == VERTICAL_BOTTOM){
- return sy - (this.h + bounds.top);
- }
- return sy;
- }
- public void draw() {
- draw(0, 0);
- }
- public void draw(float mx, float my) {
- if (canvas == null) return;
- if (!visible) return;
- if (!alive) return;
- float sx = mx + x;
- float sy = my + getY(y);
- float sx1 = sx + w;
- float sy1 = sy + h;
- //if off screen don't draw
- if (sx > Settings.targetW) return;
- if (sx1 < 0) return;
- if (sy > Settings.targetH) return;
- if (sy1 < 0) return;
- outline.setColor(color2);
- canvas.drawText(text, sx, sy, outline);
- paint.setColor(color1);
- canvas.drawText(text, sx, sy, paint);
- }
- }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
Initialize the objects like this:
Using java Syntax Highlighting
- txtPass = new ETFader(canvas, "Pass", scx, 30, 64, Color.parseColor("#FF660000"), Color.parseColor("#FFFF3300"));
- txtTurn = new ETFader(canvas, "Left Player's Turn", scx, 60, 48, Color.parseColor("#FF666600"), Color.parseColor("#FFCCFF00"));
- txtRound = new ETFader(canvas, "Round 1", scx, 90, 64, Color.parseColor("#FFFF3300"), Color.parseColor("#FFCCFF00"));
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Int the main loop they are drawn like this:
Using java Syntax Highlighting
- txtRound.draw();
- txtTurn.draw();
- txtPass.draw();
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Any thoughts why the colors doesn't get applied?


