Animate rotation of an image in Android

后端 未结 4 564
暗喜
暗喜 2021-01-14 08:08
  • I have a gear image which I want to continuously rotate about a fixed point.

  • Earlier I was accomplishing this by including the image in my Android

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-14 08:58

    In your onCreate() do

    Matrix matrix = new Matrix();
    

    And in onDraw

    float angle = (float) (System.currentTimeMillis() % ROTATE_TIME_MILLIS) 
       / ROTATE_TIME_MILLIS * 360;
    matrix.reset();
    matrix.postTranslate(-source.getWidth() / 2, -source.getHeight() / 2);
    matrix.postRotate(angle);
    matrix.postTranslate(centerX, centerY)
    canvas.drawBitmap(source, matrix, null);
    invalidate(); // Cause a re-draw
    

    ROTATE_TIME_MILLIS is the full circle time, e.g. 2000 is 2 seconds.

提交回复
热议问题