Breaking News


Friday 10 January 2014

LIVE WALLPAPER ANDROID TUTORIAL

Android Live Wallpaper Tutorial


Live wallpapers   - are  richer, animated, interactive backgrounds on Homescreen.

In this post I will describe step by step "How to Create LiveWallpaper " .

  The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.

 

Android Live Wallpaper Example


In this example I have created a LiveWallpaper in which a Fish  is moving left to right.
The example is too simple and has been described in manner that you can easily  understand.

Android Live Wallpaper



How to create a Live Wallpaper


Step 1: create a xml file which will describe your wallpaper.

Step 2: edit your manifest file add service and feature.

Step 3: create a service class which extends WallpaperService class.


Step 1:  create a xml folder inside res foldeR


 inside xml folder create mywallpaper.xml file


           <?xml version="1.0" encoding="UTF-8"?>


  <wallpaper
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     android:thumbnail="@drawable/fish"
                     android:description="@string/wallpaper_description"
            /> 

here thumbnail is the image that will be shown in list of livewallpapers and description attribute is the description of your live wallpaper.

Also add following in your string.xml file inside values folder
<string name="wallpaper_description">Fish Aquarium</string> 

Step 2: Edit your Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.livewallpapertutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <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_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name="LiveWallpaperService"
            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>

</manifest>



Step 3: create a 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 LiveWallpaperService extends WallpaperService
{
                int x,y;
              
                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>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)

Android Live Wallpaper
          










Share This
Blogger
Facebook
Disqus

comments powered by Disqus

No comments :

Post a Comment

Subscribe
Labels
Popular Posts

Subscribe Via Email

About Us

THIS IS ANDROID AND JAVA FREE TUTORIAL AND ALL PROVIDE SOLUTION IN ANDROID AND JAVA INTERVIEW QUE AND ANS IS AVAILABLE IN MY BLOG AND ANY QUERY PLEASE CONTACT ME GO NOW CONTACT US PAGE.

Total Pageviews

© Android and Java solution All rights reserved | Designed By Fireandroids