Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.12 KB  |  hits: 52  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android page curl by harism , adding TextView caused and exception :
  2. public interface TextProvider{
  3.         public int getTextCount();
  4.         public void setText(int index);
  5.  
  6.         public String getText(int index);
  7.  
  8.         public TextView getTextView();
  9.     }
  10.        
  11. private int[] mBitmapIds = {};
  12.  
  13.  
  14.     public void setImageList( int[] value )
  15.     {
  16.         mBitmapIds = value;
  17.     }
  18.  
  19.  
  20.     public BitmapDrawable writeOnDrawable( int drawableId, String text, int x, int y, int width )
  21.     {
  22.  
  23.         Bitmap bitmap = BitmapFactory.decodeResource( getResources(), drawableId ).copy( Bitmap.Config.ARGB_8888, true );
  24.  
  25.         Bitmap newBitmap = Bitmap.createBitmap( bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888 );
  26.         Canvas canvas = new Canvas( newBitmap );
  27.         canvas.drawBitmap( bitmap, new Matrix(), null);
  28.  
  29.         if( text != "" ){
  30.             TextView tv = new TextView( getApplicationContext() );
  31.             tv.setText( text );
  32.             tv.setTextColor( 0xa00050ff );
  33.             tv.setTextSize( 24 );
  34.             tv.setTypeface( typeface );
  35.  
  36.             Bitmap txtBitmap = Bitmap.createBitmap( width, 768, Bitmap.Config.ARGB_8888 );
  37.  
  38.             Canvas c = new Canvas( txtBitmap );
  39.             tv.layout( 0, 0, width, 300 );
  40.             tv.draw( c );
  41.  
  42.             canvas.drawBitmap( txtBitmap, x, y, null );
  43.         }
  44.  
  45.  
  46.         return new BitmapDrawable( newBitmap );
  47.     }
  48.  
  49.     //@Override
  50.     public Bitmap getBitmap( int width, int height, int index )
  51.     {
  52.         BitmapDrawable drawable;
  53.         Page currentPage = getCurrentPage( index );
  54.  
  55.         if( currentPage.getText() != null ){
  56.             drawable = writeOnDrawable( mBitmapIds[ index ] , currentPage.getText(), currentPage.getTextFieldX(), currentPage.getTextFieldY(), currentPage.getTextFieldWidth() );
  57.         } else {
  58.             drawable = writeOnDrawable( mBitmapIds[ index ] , "", currentPage.getTextFieldX(), currentPage.getTextFieldY(), currentPage.getTextFieldWidth() );
  59.         }
  60.  
  61.         Bitmap bitmap = drawable.getBitmap();
  62.  
  63.         return bitmap;  
  64.  
  65.     }
  66.  
  67.     //@Override
  68.     public int getBitmapCount() {
  69.         return mBitmapIds.length;
  70.     }
  71. }