创建带阴影的文本位图

时间:2014-06-18 07:34:43

标签: android text bitmap draw shadow

我想创建一个带阴影的文本位图,但是我无法获得好的结果。问题是,当我直接绘制文本时,它看起来不错,但是当我将文本绘制到位图,然后绘制位图时,它看起来很难看。

代码:

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);
        }
    }
}

useBitmap设置为false时,结果如下所示

enter image description here

useBitmap设置为true时,结果如下所示

enter image description here

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

质量损失似乎与位图有关。 您可以使用灰色阴影并使用更大的位图来获得更好的结果(即使它意味着在之后重新调整它)。

    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