image onDraw method don't move in runnable as i give the x-axis pixes to go ahead
I have a canvas, i have a squre on canvas.
i want to move a image on on this squre x-axis but it is stable how ever i is changed.
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.googlelogo320x480);
开发者_Python百科 // canvas.drawBitmap(myBitmap, 0, 0, null);
myPaint = new Paint();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
canvas1 = canvas;
canvas1.drawRect(80, 40, 570, 800, myPaint);
myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.horse);
canvas1.drawBitmap(myBitmap, i, 10, myPaint);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
canvas1.drawBitmap(myBitmap, i, 10, myPaint);
System.out.println(i);
if(i<550){
handler.postDelayed(this, 100);
i++;
}
}
};
handler.postDelayed(runnable, 1000);
}
}
what should i do to move the image on canvas....
thanks in advance.
onDraw
is called every time the OS wants to draw the image on screen. Thus, you do not need to have a runnable loop like that because you may have multiple loops running and drawing the screen.
You can try this. Remove the handler and loop. Instead, make i
private in the class and increase it by 1 every time onDraw
is called. Then reset i
when you've reached its max. Call invalidate()
at the end of the method. This will tell the OS to redraw the image again when it is ready.
精彩评论