8

I want to create a bitmap of text with shadow, but I can't get good result. The problem is, when I directly draw the text, it looks good, but when I draw the text to a bitmap, and then draw the bitmap, it looks ugly.

Code:

public class MyView extends View {
    private Paint paint;
    private Bitmap bitmap;

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init(){
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(50);
        paint.setAntiAlias(true);
        paint.setTypeface(Typeface.create("HELVETICA", Typeface.NORMAL));
        paint.setShadowLayer(30, 0, 0, Color.BLACK);

        bitmap = Bitmap.createBitmap(500, 300, Bitmap.Config.ARGB_8888); 
        Canvas canvas2 = new Canvas(bitmap);
        canvas2.drawText("Dec Use", 100, 100, paint);
    }

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

        final boolean useBitmap = true;
        if(useBitmap){
            canvas.drawBitmap(bitmap, 0, 0, null);
        }
        else{
            canvas.drawText("Dec Use", 100, 100, paint);
        }
    }
}

When useBitmap is set to false, the result looks like this

enter image description here

When useBitmap is set to true, the result looks like this

enter image description here

Am I missing something?

Cosyn
  • 4,404
  • 1
  • 33
  • 26

1 Answers1

6

The loss of quality seems to be related to the bitmap. You can get a better result by using a grey shadow and using bigger bitmaps (even if it means resing it after).

    bitmap = Bitmap.createBitmap(2000, 2000, Bitmap.Config.ARGB_8888); 
    Canvas canvas2 = new Canvas(bitmap);
    canvas2.drawText("Dec Use", 200, 200, paint);

    paint.setShadowLayer(20, 0, 0, Color.GRAY);
    canvas2.drawText("Dec Use", 200, 200, paint);

enter image description here

Related answer

Community
  • 1
  • 1
Helix
  • 1,259
  • 1
  • 9
  • 14
  • Grey color makes the shadow dimmer, so it looks better. but I don't think it actually solves this problem. – Cosyn Jun 18 '14 at 16:06
  • The problem is the quality of the bitmap, which can be solved with bigger bitmaps (or a Bitmap.Config trick but I don't think so). – Helix Jun 20 '14 at 08:08