2016-11-29 100 views
0

我的代碼是在這裏是否可以在畫布上的drawtext上添加光標?

textPaint.setStyle(Paint.Style.FILL); 
textPaint.setTextAlign(Paint.Align.CENTER); 
canvas.drawText(substring, textX, y, this.textPaint); 

我想在這裏補充也光標一樣的EditText

+0

你爲什麼不使用風格的'EditText'? –

+0

@DimaRostopira因爲我想在點擊的位置上添加文本,所以我必須使用畫布作爲另一個繪圖元素一起可以工作 – hugerde

回答

0

沒有,據我知道這是不可能的,因爲文字是一個位圖的一部分,而不是解釋爲文本通過文本標記界面。

我建議另一種解決方案:通過調用

  1. 使用封裝佈局結構類似(佈局僅simplyfied!)

    <FrameLayout> 
        <Canvas /> 
        <TextView /> 
        <!-- More textviews --> 
    </FrameLayout> 
    
  2. 您可以定位的FrameLayout裏面的TextView功能setXsetY。只要textview在畫布之後定義,textview將始終繪製在畫布的頂部。此外,您還可以通過調用函數setVisibility使其可見/不可見的代碼

  3. 除此之外,您可以添加更多Textviews到的FrameLayout動態使用代碼,而不是在佈局XML靜態定義的TextView

0

可以繪製閃爍的光標這樣說:

private long lastCursorChangeState = -1; 
private boolean cursorVisible = true; 
private Rect textBounds = new Rect(); 

@Override 
protected void onDraw(Canvas canvas) { 
    if(isWriting){ 
     if(System.currentTimeMillis() - lastCursorChangeState > 500) { 
      cursorVisible = !cursorVisible; 
      lastCursorChangeState = System.currentTimeMillis(); 
     } 

     if(cursorVisible){ 
      paint.getTextBounds(textToDraw, 0, textToDraw.length(), textBounds); 
      canvas.drawLine(textX+textBounds.right, textY-textSize, textX+textBounds.right, textY, paint); 
     } 

     postInvalidateDelayed(500); 
    } 
} 
相關問題