2014-06-13 61 views
0

我是一名android開發人員。我正在做一個遊戲,我需要改變我的字母樣式,如下圖所示。如何在畫布添加漸變如圖片letters將漸變設置爲Canvas.drawtext

將此代碼添加漸變,而是整個視圖。如果在底部的信件裏,他們是白色的,如果在頂部的信件他們是黑人

Paint p = new Paint(); 
// start at 0,0 and go to 0,max to use a vertical 
// gradient the full height of the screen. 
p.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR)); 
canvas.drawPaint(p); 
+0

使用Paint.setShader – pskink

+0

是的,我這樣做,而我得到的是整個觀點是影響。只有在某些部分視圖中,字母纔會受到影響。我只想要字母受梯度影響 – user3189504

+0

發佈您的代碼然後 – pskink

回答

1

嘗試這樣的事:

class V extends View { 
    private String text = "Back To The Future"; 
    private Paint paint; 
    public V(Context context) { 
     super(context); 
     paint = new Paint(); 
     paint.setTextSize(32); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     Rect bounds = new Rect(); 
     paint.getTextBounds(text, 0, text.length(), bounds); 
     Shader shader = new LinearGradient(0, paint.getTextSize() + bounds.top, 0, paint.getTextSize(), Color.RED, Color.YELLOW, TileMode.MIRROR); 
     paint.setShader(shader); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawText(text, 0, paint.getTextSize(), paint); 
    } 
} 
+0

@ user3189504是否有效? – pskink