2014-06-18 44 views
2

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

代码:

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

我错过了什么吗?

+0

有人找到解决办法? – dakshbhatt21

回答

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

+0

灰色使阴影变暗,因此看起来更好。但我不认为它实际上解决了这个问题。 – Cosyn

+0

问题是位图的质量,可以用更大的位图解决(或Bitmap.Config技巧,但我不这么认为)。 – Helix