Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. int currentAnimationFrame = 0;
  2.  
  3. int visImage[] = { R.drawable.img_1, R.drawable.img_2, R.drawable.img_3,
  4. R.drawable.img_4, R.drawable.img_5, R.drawable.img_6,
  5. R.drawable.img_7 };
  6.  
  7. class AnimationThread extends Thread {
  8.  
  9. private boolean mRun;
  10. private SurfaceHolder mSurfaceHolder;
  11.  
  12. public AnimationThread(SurfaceHolder surfaceHolder) {
  13. mSurfaceHolder = surfaceHolder;
  14.  
  15. }
  16.  
  17. @Override
  18. public void run() {
  19. while (mRun) {
  20. Canvas c = null;
  21. try {
  22. c = mSurfaceHolder.lockCanvas(null);
  23. synchronized (mSurfaceHolder) {
  24. doDraw(c);
  25. }
  26. } finally {
  27. if (c != null) {
  28. mSurfaceHolder.unlockCanvasAndPost(c);
  29. }
  30. }
  31. }
  32. }
  33.  
  34. private void doDraw(Canvas canvas) {
  35. currentAnimationFrame++;
  36. if (currentAnimationFrame >= visImage.length) {
  37. currentAnimationFrame = 0;
  38. }
  39.  
  40. Bitmap background = BitmapFactory.decodeResource(getResources(),
  41. visImage[currentAnimationFrame]);
  42. canvas.drawBitmap(background, 0, 0, null);
  43. canvas.restore();
  44. }
  45.  
  46. public void setRunning(boolean b) {
  47. mRun = b;
  48. }
  49.  
  50. }
  51.  
  52. private AnimationThread thread;
  53.  
  54. public AnimatedSurface(Context context, AttributeSet attrs) {
  55. super(context, attrs);
  56. SurfaceHolder holder = getHolder();
  57. holder.addCallback(this);
  58. thread = new AnimationThread(holder);
  59. }
  60.  
  61. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  62. int height) {
  63.  
  64. }
  65.  
  66. public void surfaceCreated(SurfaceHolder holder) {
  67. thread.setRunning(true);
  68. thread.start();
  69. }
  70.  
  71. public void surfaceDestroyed(SurfaceHolder holder) {
  72. boolean retry = true;
  73. thread.setRunning(false);
  74. while (retry) {
  75. try {
  76. thread.join();
  77. retry = false;
  78. } catch (InterruptedException e) {
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement