2016-12-18 41 views
2

我正在嘗試爲我的android應用創建自定義視圖。在OnDraw函數中,我試圖通過使用其unicode值來繪製表情符號,但這似乎不起作用。以下是代碼:使用unicode值在Android畫布上繪製表情符號

public class Scale extends View { 
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    private final static int LINE_WIDTH = 10; 
    ... 
    ... 
    @Override 
    protected void onDraw(final Canvas canvas) { 
     super.onDraw(canvas); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeWidth(LINE_WIDTH); 
     mPaint.setColor(Color.BLUE); 
     ... 
     ... 
     //This works 
     canvas.drawText("My text", 0.05f*width, 0.80f*height, mPaint); 
     //But this does NOT draw a doughnut!! 
     String s = new String(Character.toChars(0x1F369)); //Doughnut 
     canvas.drawText(s, 0.75f*width, 0.50f*height, mPaint); 
    } 
} 

任何人都知道是否有任何解決方法在這裏?或者我做錯了什麼?

EDIT [第二個問題]:隨着我下面提出的黑客,我看到了表情符號都在Canvas繪製TextView內呈現,但他們是顯著相比,表情符號,在正常的TextView設置暗淡 ,如下圖所示:

enter image description here

任何想法,我缺少什麼嗎?

回答

1

對於它的價值,我已經找到了這種黑客攻擊,這是我自己不喜歡了!在此,我創建一個Layout(例如LinearLayout)並添加一個包含我的表情符號的TextView,然後在Canvas上繪製Layout

public class Scale extends View { 
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    private final LinearLayout layout; 
    ... 
    ... 
    public Scale(final Context context, ...) { 
     super(context); 
     ... 
     ... 
     //Initialise the layout & add a TextView with the emoji in it 
     layout = new LinearLayout(context); 
     final TextView tv = new TextView(context); 
     tv.setText(new String(Character.toChars(0x1F369))); //Doughnut 
     layout.addView(tv); 
     layout.measure(50, 50); 
     layout.layout(0, 0, 50, 50); 
    } 
    @Override 
    protected void onDraw(final Canvas canvas) { 
     super.onDraw(canvas); 
     ... 
     ... 
     canvas.translate(20, 20); //Translate if necessary 
     //Draw the layout on the canvas, draws a doughnut!! 
     layout.draw(canvas); 
     canvas.save(); 
     canvas.restore(); 
    } 
} 

請發帖,如果有更好的解決方案。

編輯

我想通了,StaticLayout是一個更好的選擇在畫布上繪製& 文本沒有文字/表情符號的問題變得暗淡

我的修改後的代碼(線比以前更少的):

public class Scale extends View { 
    private final TextPaint tPaint = new TextPaint(); 
    private final StaticLayout lsLayout; 
    ... 
    ... 
    public Scale(final Context context, ...) { 
     super(context); 
     ... 
     ... 
     //Initialise the layout & add a TextView with the emoji in it 
     String emoji = new String(Character.toChars(0x1F369))); //Doughnut 
     lsLayout = new StaticLayout(emoji, tPaint, 80, Layout.Alignment.ALIGN_CENTER, 1, 1, true); 
    } 
    @Override 
    protected void onDraw(final Canvas canvas) { 
     super.onDraw(canvas); 
     ... 
     ... 
     canvas.translate(20, 20); //Translate if necessary 
     //Draw the layout on the canvas, draws a doughnut as bright as the rest of the canvas!! 
     lsLayout.draw(canvas); 
     canvas.save(); 
     canvas.restore(); 
    } 
} 

下面是結果時,表情符號是作爲圖中在畫布上的其餘部分一樣明亮:

enter image description here

0

的問題是你的表情符號是0000的範圍之外 - FFFF 所以它不能被表示爲一個單一的Unicode字符。

如果您正確編碼表情符號的「unicode surrogate pairs」,這可能是可能的。

有在確定正確的替代,對mulitbyte的Unicode 的計算器:http://www.russellcottrell.com/greek/utilities/surrogatepaircalculator.htm

+0

由於,但這似乎沒有幫助: 'String s = new String(Character.toChars(0xD83C))+ new String(Character.toChars(0xDF69)); // Donut canvas.drawText(s,0.75f * width,0.50f * height,mPaint);' – Curious