Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a Live Wallpaper

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 574
    Comment on it

    Follow the below steps to create Live Wallpaper

    1. create a xml file which will describe your wallpaper screen.
    2. edit manifest file and add service code
    3. create a service class which extends WallpaperService class

  • create xml folder inside res folder
  • <!--?xml version="1.0" encoding="UTF-8"?-->
           <wallpaper
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     android:thumbnail="@drawable/ic_launcer"
                     android:description="@string/wallpaper_description" 
           /> 
    

  • Edit your manifest file
  • <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    
     <uses-feature
        android:name="android.software.live_wallpaper"
        android:required="true" >
    </uses-feature>
    
    <application
        android:icon="@drawable/ic_auncher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name="MyLiveWallpaperService"
            android:enabled="true"
            android:label="Wallpaper Example "
            android:permission="android.permission.BIND_WALLPAPER" >
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" >
                </action>
            </intent-filter>
    
            <meta-data
                android:name="android.service.wallpaper"
                android:resource="@xml/mywallpaper" >
            </meta-data>
        </service>
    
    </application>
    

  • create service class which extends WallpaperService
  • this class is the base class for all live wallpapers in the system. You must implement the onCreateEngine() method . The Engine class defines the life cycle methods, as for example onCreate(), onSurfaceCreated(), onVisibilityChanged(), onOffsetsChanged(), onTouchEvent() and onCommand().

    public class MyLiveWallpaperService extends WallpaperService
    {
                    int x,y;</p>
    
              public void onCreate()
                {
                        super.onCreate();
                }
    
                public void onDestroy()
                {
                        super.onDestroy();
                }
    
                public Engine onCreateEngine()
                {
                        return new MyWallpaperEngine();
                }
    
                class MyWallpaperEngine extends Engine
                {
    
                        private final Handler handler = new Handler();
                        private final Runnable drawRunner = new Runnable() {
                            @Override
                            public void run() {
                                draw();
                            }
                        };
                        private boolean visible = true;
                        public Bitmap image1,backgroundImage;
    
                        MyWallpaperEngine()
                        {
                                 // get the fish and background image references
                                image1 = BitmapFactory.decodeResource(getResources(),R.drawable.fish);
                                backgroundImage = BitmapFactory.decodeResource(getResources(),R.drawable.background);
                                x=-130; // initialize x position
                                y=200;  // initialize y position
    
                        }
    
    
                        public void onCreate(SurfaceHolder surfaceHolder)
                        {
                                super.onCreate(surfaceHolder);
                        }
    
                        @Override
                        public void onVisibilityChanged(boolean visible)
                        {
                                this.visible = visible;
                                // if screen wallpaper is visible then draw the image otherwise do not draw
                                if (visible)
                                {
                                    handler.post(drawRunner);
                                }
                                else
                                {
                                    handler.removeCallbacks(drawRunner);
                                }
                        }
    
                        @Override
                        public void onSurfaceDestroyed(SurfaceHolder holder)
                        {
                                super.onSurfaceDestroyed(holder);
                                this.visible = false;
                                handler.removeCallbacks(drawRunner);
                        }
    
                        public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels)
                        {
                                draw();
                        }
    
                        void draw()
                        {
                                final SurfaceHolder holder = getSurfaceHolder();
    
                                Canvas c = null;
                                try
                                {
                                        c = holder.lockCanvas();
                                        // clear the canvas
                                        c.drawColor(Color.BLACK);
                                        if (c != null)
                                        {
                                                // draw the background image
                                                c.drawBitmap(backgroundImage, 0, 0, null);
                                                // draw the fish
                                                c.drawBitmap(image1, x,y, null);
                                                // get the width of canvas
                                                int width=c.getWidth();
    
                                                // if x crosses the width means  x has reached to right edge
                                                if(x&gt;width+100)
                                                {  
                                                        // assign initial value to start with
                                                        x=-130;
                                                }
                                                // change the x position/value by 1 pixel
                                                x=x+1;
                                        }
                                 }
                                finally
                                {
                                        if (c != null)
                                               holder.unlockCanvasAndPost(c);
                                }
    
                                handler.removeCallbacks(drawRunner);
                                if (visible)
                                {
                                          handler.postDelayed(drawRunner, 10); // delay 10 mileseconds
                                }   
    
                        }
                }
    
    }
    

  • Run the application and set the Wallpaper.( Long Press on Screen ->Set Wallpaper -> HomeScreen -> Live Wallpapers -> select your Live wall paper)
Comment on it

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: