0

I used the following code to draw text on a view canvas:

Paint paint = new Paint();
paint.setTextSize(14);
paint.setColor(0xFFFFFFFF);
paint.setAntiAlias(true);

@Override
public void onDraw(Canvas canvas){
   super.onDraw(canvas); 

   canvas.drawText("abcdef", 0, 0, paint);
}

Question:

The text is displayed in a dark grey color instead of white color (0xFFFFFFFF). Why so?

I tried to use setShadowLayer() or paint.setAntiAlias(false) in order to solve the problem, unfortunately without success.

Loktar
  • 34,764
  • 7
  • 90
  • 104
Anne Droid
  • 3,151
  • 4
  • 16
  • 15
  • use gimp or photoshop to get the exact color of ur choice :) – Charan Pai Dec 22 '12 at 12:03
  • The problem is not the color code. 0xFFFFFFFF is white and I want to use white. The display of my Tablet shows nevertheless the text in grey and not in white. Please do not make a proposal now, that I should increase the contrast of my Tablet :-) – Anne Droid Dec 22 '12 at 12:26

5 Answers5

1

Set this in res/value

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
</resources>

Then put this in your code-

int myColor = context.getResources().getColor(com.example.test.R.color.white);
paint.setColor(myColor);
mihirjoshi
  • 12,161
  • 7
  • 47
  • 78
  • Thx, but that doesn't solve the problem. You take the color information from the (xml) resources. It makes no difference if you take the color information from the resources or you set it manually in the code. Do you have another proposal ? – Anne Droid Dec 22 '12 at 11:58
1

There must be some other place where your paint color (or alpha?) is being modified. I implemented your code as follows, and the text color is white.

private void drawText() {
    LinearLayout layout = (LinearLayout) findViewById(R.id.main_view);
    if (layout != null){
        View myView = new MyView(this);
        myView.setBackgroundColor(Color.BLACK);
        layout.addView(myView, 300, 100);
    }
}

private class MyView extends View{

    public Paint paint;

    public MyView(Context context) {
        super(context);

        paint = new Paint();
        paint.setTextSize(25);
        paint.setColor(0xFFFFFFFF);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas){
           super.onDraw(canvas); 
           canvas.drawText("abcdef", 0, 25, paint);
    }
}

enter image description here

bobnoble
  • 5,794
  • 3
  • 25
  • 32
0

Color set like this in place of your code use this

        Paint Paint = new Paint();
        Paint.setAntiAlias(true);
        Paint.setDither(true);
        Paint.setColor(Color.WHITE);
        Paint.setStyle(Paint.Style.STROKE);
        Paint.setStrokeJoin(Paint.Join.ROUND);
        Paint.setStrokeCap(Paint.Cap.ROUND);
        Paint.setStrokeWidth(10);

see here is good example blogspot.in

NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • Thanks, I have tested your proposal, but the effect remains the same. The text is displayed in grey and not in white. – Anne Droid Dec 22 '12 at 12:23
0

I am not sure whether android support this hex code of color ( 0xFFFFFFFF ), replace 0x to # of 0xFFFFFFFF and use

paint.setColor(Color.parseColor("#FFFFFFFF"));

instead of

paint.setColor(0xFFFFFFFF);

Hope this helps you.

vinay kumar
  • 1,451
  • 13
  • 33
  • Yes, Android supports hex color codes. Color.parseColor() is not needed here. I tested your proposal, unfortunately without success. – Anne Droid Dec 22 '12 at 12:20
  • Please refer this for more info http://stackoverflow.com/questions/5248583/how-to-get-a-color-from-hexadecimal-color-string – vinay kumar Dec 22 '12 at 12:26
-1

After some investigations, I found the problem and the solution:

My mistake was, that I used in addition to the onDraw() method a semi-transparent background color setBackgroundColor(0x88000000) in a child view, which leads to the strange results, i.e. grey text color instead of white text color. The text color is now white, when I remove the method setBackgroundColor() from the child view.

Thank you all for your ideas and your proposals !

Anne Droid
  • 3,151
  • 4
  • 16
  • 15