2011-08-29 6 views
7

Estoy en el proceso de desarrollo de una aplicación de lector de libros electrónicos para tabletas Android 3.0. Para empezar, tengo una gran cantidad de datos de Cadena. Quiero dividir/dividir esa cadena en páginas según el tamaño de la pantalla del dispositivo [Estoy planeando utilizar el selector de texto o ver flipper]. Aunque traté de usar el método getWindowManager(), no pude obtener los resultados preferidos.Rompiendo texto grande en páginas en el selector de texto de Android o ver flipper

En el siguiente hilo se menciona que Text Switcher rompe automáticamente el texto de acuerdo con el tamaño de la pantalla. Pero no lo creo. Managing text in android applicaiton like in a eBook

Esta es la lógica utilicé:

// retreiving the flipper  
    flipper = (ViewFlipper) findViewById(R.id.new_view_flipper);   

    // obtaining screen dimensions  
    Display display = getWindowManager().getDefaultDisplay(); 
    int screenWidth = display.getWidth(); 
    int screenHeight = display.getHeight(); 

    // contentString is the whole string of the book 

    while (contentString != null && contentString.length() != 0) 
    { 
     totalPages ++; 

     // creating new textviews for every page 
     TextView contentTextView = new TextView(this); 
     contentTextView.setWidth(ViewGroup.LayoutParams.FILL_PARENT); 
     contentTextView.setHeight(ViewGroup.LayoutParams.FILL_PARENT); 
     contentTextView.setMaxHeight(screenHeight); 
     contentTextView.setMaxWidth(screenWidth); 

     float textSize = contentTextView.getTextSize(); 
     Paint paint = new Paint(); 
     paint.setTextSize(textSize); 

     int numChars = 0; 
     int lineCount = 0; 
     int maxLineCount = screenHeight/contentTextView.getLineHeight(); 
     contentTextView.setLines(maxLineCount); 

     while ((lineCount < maxLineCount) && (numChars < contentString.length())) { 
      numChars = numChars + paint.breakText(contentString.substring(numChars), true, screenWidth, null); 
      lineCount ++; 
     } 

     // retrieve the String to be displayed in the current textbox 
     String toBeDisplayed = contentString.substring(0, numChars); 
     contentString = contentString.substring(numChars); 
     contentTextView.setText(toBeDisplayed); 
     flipper.addView(contentTextView); 


     numChars = 0; 
     lineCount = 0; 
    } 
+0

Ver mi respuesta aquí http://stackoverflow.com/questions/20204348/how-to-break-styled-text-into-pages-in-android – mixel

Respuesta

2

Esto es todo lo que necesita para hacer su trabajo de código.

DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    int screenWidth = dm.widthPixels; 
    int screenHeight= dm.heightPixels; 

Reemplace su siguiente bloque de código con el mío. Funcionará.

Display display = getWindowManager().getDefaultDisplay(); 
int screenWidth = display.getWidth(); 
int screenHeight = display.getHeight(); 
Cuestiones relacionadas