Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. <pre>Im using WallpaperService to set the live wallpaper on homescreen. Im able to set 10 pics from resources and rotate them
  2.  
  3. class WallpaperEngine extends Engine {
  4. //frequency selected by user to update the wallpaper
  5. private final int Wallpaper_DURATION = utils.getFrequency;
  6.  
  7. private int[] mImagesArray;
  8. private int mImagesArrayIndex = 0;
  9. private Thread mDrawWallpaper;
  10.  
  11. public WallpaperEngine() {
  12. customWallpaperHelper = new CustomWallpaperHelper(getApplicationContext(), getResources());
  13. mImagesArray = new int[] {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
  14.  
  15. mDrawWallpaper = new Thread(new Runnable() {
  16. @Override
  17. public void run() {
  18. try {
  19. while (true) {
  20. drawFrame();
  21. incrementCounter();
  22. Thread.sleep(Wallpaper_DURATION);
  23. }
  24. } catch (Exception e) {
  25. //
  26. }
  27. }
  28. });
  29.  
  30. mDrawWallpaper.start();
  31. }
  32.  
  33. private void incrementCounter() {
  34. mImagesArrayIndex++;
  35.  
  36. if (mImagesArrayIndex >= mImagesArray.length) {
  37. mImagesArrayIndex = 0;
  38. }
  39. }
  40.  
  41. private void drawFrame() {
  42. final SurfaceHolder holder = getSurfaceHolder();
  43.  
  44. Canvas canvas = null;
  45.  
  46. try {
  47. canvas = holder.lockCanvas();
  48.  
  49. if (canvas != null) {
  50. drawImage(canvas);
  51. }
  52. } finally {
  53. if (canvas != null) {
  54. holder.unlockCanvasAndPost(canvas);
  55. }
  56. }
  57. }
  58.  
  59. private void drawImage(Canvas canvas)
  60. {
  61.  
  62. Bitmap image = BitmapFactory.decodeResource(getResources(),
  63. mImagesArray[mImagesArrayIndex]);
  64. Bitmap b=Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), true);
  65. canvas.drawBitmap(b, 0,0, null);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement