Reputation: 1980
First do look on my code.
Here's my code:-
public GameView(Context context){
super(context);
Display display = ((WindowManager)context.getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay();
}
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
int x = 0;
int y = 0;
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
int imageWidth = bmp.getWidth();
int imageHeight = bmp.getHeight();
int width = display.getWidth();
System.out.println("Width = " +width);
int height = display.getHeight();
System.out.println("Height = " +height);
Random randomX,randomY;
randomX = new Random();
randomY = new Random();
x = randomX.nextInt(width - imageWidth);
System.out.println("X = " +x);
y = randomY.nextInt(height - imageHeight);
System.out.println("Y = " +y);
Rect dst = new Rect(x , y , x + imageWidth , y + imageHeight);
canvas.drawBitmap(bmp, null , dst , null);
System.out.println("dst = " +dst);
super.draw(canvas);
}
My code is running perfectly but my problem is that the draw(Canvas canvas)
running twice.
Here's logs which i am getting them in Logcat every time with different values:-
Width=480
Height=800
X=247
Y=456
dst = Rect(247 , 456 - 319 , 528)
Width=480
Height=800
X=119
Y=560
dst = Rect(119 , 560 - 191 , 632)
Every time when I am running my application,draw(Canvas canvas) is running twice.I dont want that it'll run twice every time.Help me to get rid of this problem.Any help would be highly appreciated.
Upvotes: 3
Views: 1236
Reputation: 2161
When you override any method,better call
super.overridedMethod();
first,then code your implementation.
Try,
super.draw();
//NOW YOUR STUFF
or remove super.draw();
You better override onDraw()
method than draw()
.
Make sure that setContentView(new GameView(this));
is called in onCreate()
.
Hope this may help you
Upvotes: 1