Skip to content

Performance hints

peterLaurence edited this page Jun 20, 2016 · 3 revisions

Here we list some good practices to get the best experience with TileView.

BitmapProvider implementation

When you implement your own BitmapProvider, beware that the way tiles are effectively stored in memory may have an impact on performance. For example, if you use BitmapFactory.decodeStream method, you can specify different Bitmap configurations. The BitmapProviderAssets implementation is :

public class BitmapProviderAssets implements BitmapProvider {

  private static final BitmapFactory.Options OPTIONS = new BitmapFactory.Options();

  static {
    OPTIONS.inPreferredConfig = Bitmap.Config.RGB_565;
  }

  @Override
  public Bitmap getBitmap( Tile tile, Context context ) {
    Object data = tile.getData();
    if( data instanceof String ) {
      String unformattedFileName = (String) tile.getData();
      String formattedFileName = String.format( unformattedFileName, tile.getColumn(), tile.getRow() );
      AssetManager assetManager = context.getAssets();
      try {
        InputStream inputStream = assetManager.open( formattedFileName );
        if( inputStream != null ) {
          try {
            return BitmapFactory.decodeStream( inputStream, null, OPTIONS );
          } catch( OutOfMemoryError | Exception e ) {
            // this is probably an out of memory error - you can try sleeping (this method won't be called in the UI thread) or try again (or give up)
          }
        }
      } catch( Exception e ) {
        // this is probably an IOException, meaning the file can't be found
      }
    }
    return null;
  }
}

Here, the Bitmap configuration is RGB_565. If you don't specify any option, the Bitmap configuration will be ARGB_8888 by default, which requires twice the memory needed with RGB_565. See the official documentation for more details. On some devices, using ARGB_8888 may cause stutters during a fling.

You may not see a loss in image quality with RGB_565. In that case, there is no reason not using it.