Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:48:24+00:00 2026-06-17T04:48:24+00:00

I am trying to make an animated Android background, using a few images that

  • 0

I am trying to make an animated Android background, using a few images that I am drawing. I draw the images step by step, but the phone draws quite it slowly.

I am trying this:

    public class MyWallpaperService extends WallpaperService {
        private int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
                R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
                R.drawable.p8, R.drawable.p9, R.drawable.p10, R.drawable.p11,
                R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15,
                R.drawable.p16, R.drawable.p17, R.drawable.p18, R.drawable.p19,
                R.drawable.p20, R.drawable.p21, R.drawable.p22, R.drawable.p23,
                R.drawable.p24 };

        @Override
        public Engine onCreateEngine() {
            return new MyWallpaperEngine();
        }

        private class MyWallpaperEngine extends Engine {
            private final Handler handler = new Handler();
            private final Runnable drawRunner = new Runnable() {
                @Override
                public void run() {
                    drawImage(images[0]);
                }

            };

            private Paint paint = new Paint();
            private boolean touch = true;

            @Override
            public void onVisibilityChanged(boolean visible) {
                if (visible) {
                    handler.post(drawRunner);
                } else {
                    handler.removeCallbacks(drawRunner);
                }
            }

            @Override
            public void onSurfaceDestroyed(SurfaceHolder holder) {
                super.onSurfaceDestroyed(holder);
                handler.removeCallbacks(drawRunner);
            }

            @Override
            public void onSurfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
                super.onSurfaceChanged(holder, format, width, height);
            }

            @Override
            public void onTouchEvent(MotionEvent event) {
                if (partOne) {
                    if (touch) {
                        touch = false;
                        for (int i = 0; i < images.length; i++)
                            imagePostDelay(i, i * 100, i == images.length - 1 ? true
                                    : false);
                    }
                }
                super.onTouchEvent(event);
            }

            private void drawImage(int id) {
                WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
                Display display = wm.getDefaultDisplay();
                @SuppressWarnings("deprecation")
                int width = display.getWidth(); // deprecated 960
                @SuppressWarnings("deprecation")
                int height = display.getHeight(); // deprecated 540
                final Resources res = getResources();
                Bitmap bitmap;
                Canvas canvas = null;
                SurfaceHolder holder = getSurfaceHolder();
                bitmap = BitmapFactory.decodeResource(res, id);
                bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
                try {
                    canvas = holder.lockCanvas();
                    canvas.drawBitmap(bitmap, 0, 0, paint);
                } catch (NullPointerException e) {
                } finally {
                    if (canvas != null)
                        holder.unlockCanvasAndPost(canvas);
                }
            }

            private void imagePostDelay(final int index, int delay,
                    final boolean allowTouch) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        try {
                            drawImage(images[index]);
                        } catch (ArrayIndexOutOfBoundsException e) {
                        }
                        if (allowTouch)
                            touch = true;
                    }
                }, delay);
            }
        }

I attempt to save images in the proper resolution in the onCreate method, but it didn’t help. I then tried with less images, but it threw an OutOfMemory Exception. That might be caused by the amount of images. The main thing is that catching images doesn’t help a lot. It was still slow.

  • Are there any good tutorials that help with making live slideshow
    backgrounds on Android?
  • Is it good idea to store images in a proper resolution in a xml and
    just use the setWallpaper() method to change images? Will the
    setWallpaper() method be faster?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:48:25+00:00Added an answer on June 17, 2026 at 4:48 am

    To draw more efficiently move

    bitmap = BitmapFactory.decodeResource(res, id);
    bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
    

    to

    ....
    private class MyWallpaperEngine extends Engine {
    bitmap = BitmapFactory.decodeResource(res, id);
    
    ....
        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
    
            // size the background bitmap so it draws efficiently 
            bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
    
            drawFrame();
        }
    
    ....
    void drawFrame() {
    Canvas c = null;
            try {
                c = holder.lockCanvas();
                if (c != null) {
                    // This will draw on every cycle of the Runnable, 
                    // so don't do any decoding of Bitmap here, 
                    // that would be very inefficient.  
                   canvas.drawBitmap(bitmap, 0, 0, paint);
             }
            } finally {
                if (c != null) holder.unlockCanvasAndPost(c);
            }
    
            mHandler.removeCallbacks(drawRunner);
            if (mVisible) {
                mHandler.postDelayed(drawRunner,  1000/25);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to make a simple animated menu that slides in and out but
I am trying to make an animated clock using Swing in Java. The picture
I'm trying to learn how to make an animated sprite in android and couldn't
I'm trying to make an animated menu using :hover using two div s with
I am trying to make an animated scroll to a bottom button using jQuery
I'm trying to make a basic (non animated) slot machine but am getting an
I'm trying to make a tab bar in android using a RelativeLayout and a
I'm trying to make an animated slide show. This code works but without animation
Im trying to make an animated list that when an item is clicked, a
I am trying to make a few translations of an object using the jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.