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 8983157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:46:07+00:00 2026-06-15T20:46:07+00:00

I’m creating my first live wallpaper in which snow will produce on random location

  • 0

I’m creating my first live wallpaper in which snow will produce on random location and start animate.But it lags.
This was my code.

There is no error in the code but it lags.If i increased the number of snows,it lags more.
How to solve lag?

public class CybWallpaper extends WallpaperService {

    @Override
    public Engine onCreateEngine() {
        return new Mywall();
    }
    public class Mywall extends Engine
    {
        int x[]=new int[30],y[]=new int[30];
        int isr=0;
        int scw,sch;
        Canvas dra;
        int temps[]=new int[30];
        boolean isft=true;


        Handler hand=new Handler();
        boolean isrun;
        Random ran=new Random();
        Runnable iterate=new Runnable()
        {
            @Override
            public void run() {
                slower();
                drawframe();
            }

        };



        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            super.onSurfaceCreated(holder);


        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
            // TODO Auto-generated method stub
            super.onSurfaceChanged(holder, format, width, height);
            slower();
            drawframe();
        }

        protected void drawframe() {
            // TODO Auto-generated method stub
            SurfaceHolder sh=getSurfaceHolder();


               dra = null;
               try {
                  dra = sh.lockCanvas();
                  if (dra != null) {

                        for(int i=0;i<30&&isft;i++)
                        {
                            x[i]=ran.nextInt(dra.getWidth());
                            y[i]=ran.nextInt(dra.getHeight());
                            temps[i]=30+ran.nextInt(100);

                        }
                        isft=false;
                        dra.drawARGB(255, 0,0, 0);
                        drawsnow(dra,30);

                  }
               } finally {
                  if (dra != null)
                     sh.unlockCanvasAndPost(dra);
               }
        }

        private void drawsnow(Canvas dra,int n) {
            // TODO Auto-generated method stub
            Bitmap snow=BitmapFactory.decodeResource(getResources(), R.drawable.snow80);
            Bitmap te[]=new Bitmap[30];


            for(int i=0;i<n;i++)
            {
                te[i]=Bitmap.createScaledBitmap(snow, temps[i], temps[i], false);
                dra.drawBitmap(te[i], x[i]-(te[i].getWidth()/2), y[i]-(te[i].getHeight()/2), null);
                //Snow to and fro
                if(isr==0&&x[i]!=140)
                    x[i]+=1;
                else
                    isr=1;

                if(isr==1&&x[i]!=120)
                    x[i]-=1;
                else
                    isr=0;

                if(y[i]<dra.getHeight())
                    y[i]+=6;
                else
                {
                    y[i]=0;
                    x[i]=ran.nextInt(dra.getWidth());
                    temps[i]=30+ran.nextInt(100);
                }
            }

            //Toast.makeText(getApplicationContext(), Float.toString(cx), Toast.LENGTH_SHORT).show();               

        }

        protected void slower() {
            // TODO Auto-generated method stub
            hand.removeCallbacks(iterate);
             if (isrun) {
                hand.postDelayed(iterate, 1000/30);
             }

        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            super.onSurfaceDestroyed(holder);
            isrun=false;
            hand.removeCallbacks(iterate);
            Toast.makeText(getApplicationContext(), "Destroyed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            super.onTouchEvent(event);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            // TODO Auto-generated method stub
            super.onVisibilityChanged(visible);
            isrun=visible;
            if(isrun)
            {
                slower();
                drawframe();
            }
            else
            {
                hand.removeCallbacks(iterate);
            }
            //Toast.makeText(getApplicationContext(), "Visi", Toast.LENGTH_SHORT).show();
        }

    }

}

Did i coded correctly?

  • 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-15T20:46:09+00:00Added an answer on June 15, 2026 at 8:46 pm
                Bitmap snow=BitmapFactory.decodeResource(getResources(), R.drawable.snow80);
                Bitmap te[]=new Bitmap[30];
    

    I think you do something wrong here. Your code continuously decodeResource ==> waste a lot of memory and CPU ==> make your program slow, instead of doing this, you should declare a global “snow” variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.