[EDITED] Changes in code.
There is a tile set with 24 frame. how can i make animation from them getting one after another dynamicly? I tried this, but no effect at all:
ImageView image = (ImageView)findViewById(R.id.imageView);
Bitmap TileSet = BitmapFactory.decodeResource(getResources(), R.drawable.Image);
Bitmap frame = Bitmap.createBitmap(256, 128, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(frame);
for (int i = 10, width, height; i < 24; i++) {
width = i/4;
height = i%4;
try {
Thread.sleep(125);
} catch (InterruptedException e) {
e.printStackTrace();
}
canvas.drawBitmap(TileSet
, new Rect(256 * width, 128 * height
, 256 * (width+1), 128 * (height+1))
, new Rect(0, 0, image.getWidth(), image.getHeight())
, null);
image.setImageBitmap(frame);
}
The idea is to draw this animation 1 time and close this activity forever. Thats why i cant understand how to use onDraw function here.
[EDITED 2]
private void startAnimating() {
Bitmap frame;
long beginLoopTime = 0;
for (int i = 0, width, height; i < 32; i++) {
width = i/8;
height = i%8;
while(System.currentTimeMillis() - beginLoopTime < DURATION) {}
frame = Bitmap.createBitmap(TileSet, 256 * width, 128 * height, 256, 128);
final BitmapDrawable temp = new BitmapDrawable(getResources(), frame);
image.post(new Runnable() {
@Override
public void run() {
image.setBackground(temp);
image.invalidate();
}
});
beginLoopTime = System.currentTimeMillis();
}
}
This function draws only last frame. I tried to use onWindowFocusChanged...same effect.