2011-05-12 9 views
6

que estaba tomando un vistazo a la muestra el bloc de notas en el SDK de Android ver aquí: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.htmlDibujar varias líneas en el texto de edición, p. Ej. el bloc de notas

cosa es que sólo dibuja la línea actual del cursor se encuentra en, por ejemplo http://cdn2.staztic.com/screenshots/simple-notepad-app-al-1.jpg

Pero me gustaría mostrar líneas que llenan la pantalla, por ejemplo http://www.itismyworld.info/wp-content/uploads/2010/03/AK-notebook.png

Cualquier sugerencia sería genial. El código de código relevante parece estar aquí:

protected void onDraw(Canvas canvas) { 

     // Gets the number of lines of text in the View. 
     int count = getLineCount(); 

     // Gets the global Rect and Paint objects 
     Rect r = mRect; 
     Paint paint = mPaint; 

     /* 
     * Draws one line in the rectangle for every line of text in the EditText 
     */ 
     for (int i = 0; i < count; i++) { 

      // Gets the baseline coordinates for the current line of text 
      int baseline = getLineBounds(i, r); 

      /* 
      * Draws a line in the background from the left of the rectangle to the right, 
      * at a vertical position one dip below the baseline, using the "paint" object 
      * for details. 
      */ 
      canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); 
     } 

     // Finishes up by calling the parent method 
     super.onDraw(canvas); 
    } 

Respuesta

2

quizás después de ese ciclo, se calculan * líneas adicionales estimadas.

getHeight() devolverá la altura de EditarTexto en píxeles getLineHeight() será altura de una línea estándar

así getHeight/getlineHeight-getCount será el número de líneas de izquierda a dibujar.

no se puede usar getLineBounds, usando las funciones anteriores se puede calcular la posición de las líneas restantes para dibujar.

* Estimado porque el formateo del texto podría cambiar la altura de la línea, pero como no hay texto en estas líneas, eso no debería ser un problema. Pero por esa misma razón, solo debe dibujar las líneas restantes, y no usar esto para dibujar todas las líneas.

+0

+1 buena explicación .... –

31

Este es el código, basado en la sugerencia jkhouws1 's y Google de note editor

public class LinedEditText extends EditText { 
    private Rect mRect; 
    private Paint mPaint; 

    // we need this constructor for LayoutInflater 
    public LinedEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     mRect = new Rect(); 
     mPaint = new Paint(); 
     mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
     mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR HERE 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     //int count = getLineCount(); 

     int height = getHeight(); 
     int line_height = getLineHeight(); 

     int count = height/line_height; 

     if (getLineCount() > count) 
      count = getLineCount();//for long text with scrolling 

     Rect r = mRect; 
     Paint paint = mPaint; 
     int baseline = getLineBounds(0, r);//first line 

     for (int i = 0; i < count; i++) { 

      canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); 
      baseline += getLineHeight();//next line 
     } 

     super.onDraw(canvas); 
    } 
} 

En Eclipse IDE pulse Ctrl + Shift + O para añadir todas las importaciones necesarias

+0

1 implementado la respuesta de jkhouw1 correctamente. trabajando como se desee –

+0

¿Es posible aumentar el espacio entre líneas? Si hago 'baseline + = getLineHeight() + 30', hay espacio entre líneas pero cuando presiono ENTER el cursor no está sobre la línea. Está en el medio –

+0

Esto me ayuda. Para poner el uso de líneas borrosas use el siguiente código: PathEffect effects = new DashPathEffect (new float [] {3, 3, 3, 3}, 1); \t \t mPaint.setPathEffect (efectos); En el constructor LinedEditText. –

3

Creo que esto es lo que necesita :

public class LinedEditText extends EditText { 

    private static Paint linePaint; 

    static { 
     linePaint = new Paint(); 
     linePaint.setColor(Color.BLACK); 
     linePaint.setStyle(Style.STROKE); 
    } 

    public LinedEditText(Context context, AttributeSet attributes) { 
     super(context, attributes); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     Rect bounds = new Rect(); 
     int firstLineY = getLineBounds(0, bounds); 
     int lineHeight = getLineHeight(); 
     int totalLines = Math.max(getLineCount(), getHeight()/lineHeight); 

     for (int i = 0; i < totalLines; i++) { 
      int lineY = firstLineY + i * lineHeight; 
      canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint); 
     } 

     super.onDraw(canvas); 
    } 


} 
-1
<com.example.goh2.pronoornotepad.LinedEditText 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#ffffcc4b" 
     android:gravity="top|left" 
     android:singleLine="false" 
     android:text="" 
    /> 

El XML anterior funciona con el código de Max4ever's answer:

public class LinedEditText extends EditText { 
    private Rect mRect; 
    private Paint mPaint; 

// we need this constructor for LayoutInflater 
    public LinedEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    mRect = new Rect(); 
    mPaint = new Paint(); 
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
    mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR HERE 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    //int count = getLineCount(); 

    int height = getHeight(); 
    int line_height = getLineHeight(); 

    int count = height/line_height; 

    if (getLineCount() > count) 
     count = getLineCount();//for long text with scrolling 

    Rect r = mRect; 
    Paint paint = mPaint; 
    int baseline = getLineBounds(0, r);//first line 

    for (int i = 0; i < count; i++) { 

     canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); 
     baseline += getLineHeight();//next line 
    } 

    super.onDraw(canvas); 
    } 
} 
Cuestiones relacionadas