Här är större delen av min kod, så vit jag vet är det inget problem när jag ritar upp backgrunden.
Kod:
private Panel main;
public boolean start = true;
private volatile boolean running = true;
private float Scale = 0.5f;
private int BackgroundLeft = 0;
private int BackgroundUp = 0;
int width;
int height;
float TouchCordX;
float TouchCordY;
//Previous touch screen pointers position.
float oldTouchCordX;
float oldTouchCordY;
float FixValueX;
float FixValueY;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
main = new Panel(this);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//px = dp * (dpi / 160)
//dp is always 1.5.
width = (int) (4 *(metrics.density * (metrics.xdpi)));
height = (int) (4 *(metrics.density * (metrics.ydpi)));
setContentView(main,new ViewGroup.LayoutParams(320,480));
(new Thread(new AnimationLoop())).start();
}
private void DrawBackground(Canvas canvas, int ImageId) {
canvas.save();
//Preparing to scale the image to the right size.
//canvas.scale(Scale, Scale);
//getting the image from the file.
Bitmap bmp = BitmapFactory.decodeResource(getResources(), ImageId);
canvas.drawColor(Color.TRANSPARENT);
Rect src = new Rect(BackgroundLeft, BackgroundUp, BackgroundLeft + width, BackgroundUp + height);
//increase width and height here to zoom in or opposite to zoom out.
Rect dst = new Rect(0, 0, width, height);
//drawing the image. Parameter 2 and 3 are the image position on the screen, it is not the same as the canvas position on the screen.
canvas.drawBitmap(bmp, src, dst, null);
canvas.restore();
DrawText(canvas, String.valueOf(dst.bottom), 250, 180);
DrawText(canvas, String.valueOf(dst.right), 250, 200);
}
private synchronized void doDraw(Canvas canvas, Paint paint)
{
DrawBackground(canvas, R.drawable.map);
}
class Panel extends View
{
Paint paint;
public Panel(Context context)
{
super(context);
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas)
{
doDraw(canvas,paint);
}
}
class AnimationLoop implements Runnable
{
public void run()
{
while(running)
{
try
{
Thread.sleep(30);
}
catch(InterruptedException ex) {}
{
//make the object move around.
updatePhysics();
main.postInvalidate();
}
}
}
}