package org.osmdroid.views.safecanvas;

import org.osmdroid.views.overlay.Overlay;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Matrix;

/**
 * The SafeBitmapShader class is designed to work in conjunction with {@link SafeTranslatedCanvas}
 * to work around various Android issues with large canvases. For the two classes to work together,
 * call {@link #onDrawCycleStart} at the start of the {@link Overlay#drawSafe} method of your
 * {@link Overlay}. This will set the adjustment needed to draw your BitmapShader safely on the
 * canvas without any drawing distortion at high zoom levels and without any scrolling issues.
 * 
 * @see {@link ISafeCanvas}
 * 
 * @author Marc Kurtz
 * 
 */
public class SafeBitmapShader extends BitmapShader {

	private final Matrix mMatrix = new Matrix();
	
	public SafeBitmapShader(Bitmap bitmap, TileMode tileX, TileMode tileY) {
		super(bitmap, tileX, tileY);
	}

	/**
	 * This method <b>must</b> be called at the start of the {@link Overlay#drawSafe} draw cycle
	 * method. This will adjust the BitmapShader to the current state of the {@link ISafeCanvas}
	 * passed to it.
	 */
	public void onDrawCycleStart(ISafeCanvas canvas) {
		int width = canvas.getWidth();
		int height = canvas.getHeight();

		mMatrix.setTranslate(canvas.getXOffset() % width, canvas.getYOffset() % height);
		this.setLocalMatrix(mMatrix);
	}
}

@@ -861,11 +861,80 @@
 );%0D%0A
+%09private final int mBitmapWidth;%0D%0A%09private final int mBitmapHeight;%0D%0A
 %09%0D%0A
-
 %09pub
@@ -1032,24 +1032,100 @@
 X, tileY);%0D%0A
+%09%09mBitmapWidth = bitmap.getWidth();%0D%0A%09%09mBitmapHeight = bitmap.getHeight();%0D%0A
 %09%7D%0D%0A%0D%0A%09/**%0D%0A
@@ -1396,80 +1396,8 @@
  %7B%0D%0A
-%09%09int width = canvas.getWidth();%0D%0A%09%09int height = canvas.getHeight();%0D%0A%0D%0A
 %09%09mM
@@ -1437,17 +1437,24 @@
 set() %25 
-w
+mBitmapW
 idth, ca
@@ -1473,17 +1473,24 @@
 set() %25 
-h
+mBitmapH
 eight);%0D

Fix for SafeBitmapShader.