Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a bitmap of text with shadow

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?

like image 319
Cosyn Avatar asked Jun 18 '14 07:06

Cosyn


1 Answers

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

like image 137
Helix Avatar answered Sep 17 '22 11:09

Helix