-1

[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.

BeHunter
  • 123
  • 1
  • 2
  • 10
  • What about creating a runnable and changing the frames inside that – Triode Dec 09 '13 at 19:38
  • The whole activity is used only for 1 animation, and will be never used after. That`s why i think there is no need in new thread. Or can you give me more full description or link? – BeHunter Dec 10 '13 at 05:59

1 Answers1

1

You can create frame animation using xml and load the animation inside the activity. Or else create a runnable and run it inside the UI thread.

See this link to get the basic Idea about frame animation in android.

Usage of post method and runnable

Community
  • 1
  • 1
Triode
  • 11,309
  • 2
  • 38
  • 48