Tuesday 16 February 2016

Hello To Live Wallpaper: Playing With Background Color Circles

Step 1: Download the source code of  print hello - Download


Step 2: Now open directory HelloToLivewallpaper>app>src>main>java>com>example>hellotolivewallpaper>




package com.example.hellotolivewallpaper;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.util.adt.color.Color;

import java.io.IOException;

public class HelloToLiveWallpaperService extends BaseLiveWallpaperService implements IOnSceneTouchListener
{
    private static final String TAG = "HelloTo";

    private static float CAMERA_WIDTH = 480;
    private static float CAMERA_HEIGHT = 800;

    private Scene mScene;
    private ITiledTextureRegion mCircleTR;

    @Override
    public EngineOptions onCreateEngineOptions()
    {
        try
        {
            final DisplayMetrics displayMetrics = new DisplayMetrics();
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(displayMetrics);
            wm.getDefaultDisplay().getRotation();
            CAMERA_WIDTH = displayMetrics.widthPixels;
            CAMERA_HEIGHT = displayMetrics.heightPixels;
        } catch (Exception e)
        {
            Log.e(TAG, "onCreateEngineOptions " + e.toString());
        }

        //create a camera
        Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        //engine options
        return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
    {
        //create texture
        BitmapTextureAtlas textureAtlas = new BitmapTextureAtlas(getTextureManager(), 216, 72, TextureOptions.BILINEAR);
        this.mCircleTR = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textureAtlas, getAssets(), "gfx/circle_rgb.png", 0, 0, 3, 1);
        this.getEngine().getTextureManager().loadTexture(textureAtlas);

        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
    {
        //create a scene
        mScene = new Scene();

        //create a white background
        mScene.setBackground(new Background(Color.WHITE));

        //create red sprites
        AnimatedSprite mRedSprite = new AnimatedSprite(CAMERA_WIDTH / 3, CAMERA_HEIGHT / 2, mCircleTR, this.getVertexBufferObjectManager())
        {
            @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
            {
                mScene.setBackground(new Background(Color.RED));
                return true;
            }
        };

        //create green sprite
        AnimatedSprite mGreenSprite = new AnimatedSprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2, mCircleTR, this.getVertexBufferObjectManager())
        {
            @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
            {
                mScene.setBackground(new Background(Color.GREEN));
                return true;
            }
        };

        //create blue sprite
        AnimatedSprite mBlueSprite = new AnimatedSprite(2 * CAMERA_WIDTH / 3, CAMERA_HEIGHT / 2, mCircleTR, this.getVertexBufferObjectManager())
        {
            @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
            {
                mScene.setBackground(new Background(Color.BLUE));
                return true;
            }
        };

        //set particular texture tiles
        mRedSprite.setCurrentTileIndex(0);
        mGreenSprite.setCurrentTileIndex(1);
        mBlueSprite.setCurrentTileIndex(2);

        //attach our sprites to the scene
        mScene.attachChild(mRedSprite);
        mScene.attachChild(mGreenSprite);
        mScene.attachChild(mBlueSprite);

        //register sprites touch areas
        mScene.registerTouchArea(mRedSprite);
        mScene.registerTouchArea(mGreenSprite);
        mScene.registerTouchArea(mBlueSprite);

        //register scene touch listener
        mScene.setOnSceneTouchListener(this);

        pOnCreateSceneCallback.onCreateSceneFinished(mScene);
    }

    @Override
    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException
    {
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
    {
        //handling scene touch event
        if (pScene == mScene)
        {
            mScene.setBackground(new Background(Color.WHITE));
        }

        return false;
    }
}
Step 3: Now replace the code of HelloToLiveWallpaperService.java with this code.






circle_rgb.png

Step 4: Add graphical resources in  HelloToLivewallpaper>app>src>main>assets>gfx>

Run the app...

Output:


HAPPY CODING...


Setup and Create Live Wallpaper in Android Studio using AndEngine

This tutorial tells you how to Setup and Create Live Wallpaper in Android Studio using AndEngine

Prerequisite


Step 1: Start Android Studio and create new project. Fill project details and click 'Next'.





Step 2: Click 'Next'.




Step 3: Click 'Next'.




Step 4: Click 'Finish'.


Step 5: Now change project structure view(optional).




Step 6: Right click on root project and add a directory..





Step 7: Enter name for new directory(in my case its 'other_lib') and click 'OK'.


Step 8: New directory is created. Now just copy and paste downloaded AndEngine and AndEngine Live Wallpaper Extension in this directory.




Step 9: Right click on 'AndEngine' and create a new file. Enter name 'build.gradle' in the text box and click 'OK'.


 
 
 
 
 
 
 
 
 
































apply plugin: 'com.android.library'

android
        {
            compileSdkVersion 17
            buildToolsVersion "19.1"

            defaultConfig
                    {
                        minSdkVersion 14
                        targetSdkVersion 19
                    }

            compileOptions
                    {
                        sourceCompatibility JavaVersion.VERSION_1_7
                        targetCompatibility JavaVersion.VERSION_1_7
                    }

            buildTypes
                    {
                        release
                                {
                                    minifyEnabled false
                                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                                }
                    }

            sourceSets
                    {
                        main
                                {
                                    manifest.srcFile 'AndroidManifest.xml'
                                    java.srcDirs = ['src']
                                    resources.srcDirs = ['src']
                                    res.srcDirs = ['res']
                                    assets.srcDirs = ['assets']
                                }

                        instrumentTest.setRoot('HelloToLivewallpaper')
                    }
        }
Step 10: Now add this code in 'build.gradle'.




<application/>
Step 11: Now open 'AndroidManifest.xml' file which is located in 'AndEngine' and add "application closing tag".





 
 
 
 
 
 
 
 
 





































apply plugin: 'com.android.library'

android
        {
            compileSdkVersion 17
            buildToolsVersion "19.1"

            defaultConfig
                    {
                        minSdkVersion 14
                        targetSdkVersion 19
                    }

            compileOptions
                    {
                        sourceCompatibility JavaVersion.VERSION_1_7
                        targetCompatibility JavaVersion.VERSION_1_7
                    }

            buildTypes
                    {
                        release
                                {
                                    minifyEnabled false
                                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                                }
                    }

            sourceSets
                    {
                        main
                                {
                                    manifest.srcFile 'AndroidManifest.xml'
                                    java.srcDirs = ['src']
                                    resources.srcDirs = ['src']
                                    res.srcDirs = ['res']
                                    assets.srcDirs = ['assets']
                                }

                        instrumentTest.setRoot('HelloToLivewallpaper')
                    }

        }

dependencies {
    compile project(':other_lib:AndEngine-GLES2-AnchorCenter')
}
Step 12: Right click on 'AndEngine Live Wallpaper Extension' and create a new file. Enter name 'build.gradle' in the text box and click 'OK' and copy this code in it.





<application/>
Step 13: Now open 'AndroidManifest.xml' file which is located in 'AndEngine Live Wallpaper Extension' and add "application closing tag".




Step 14: Now create new directory 'xml' in app>src>main>res>





1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<wallpaper
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:thumbnail="@mipmap/ic_launcher"
    android:description="@string/wallpaper_description"
    />
Step 15: Now create new resource file 'wallpaper' in 'xml' directory and add this code in it.




Step 16: Create respective string resource(Name of our live wallpaper).





include ':app'
include ':other_lib:AndEngine-GLES2-AnchorCenter'
include ':other_lib:AndEngineLiveWallpaperExtension-GLES2-AnchorCenter'
Step 17: Now open root project's 'settings.gradle' file and add this code in it.





compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':other_lib:AndEngine-GLES2-AnchorCenter')
compile project(':other_lib:AndEngineLiveWallpaperExtension-GLES2-AnchorCenter')
Step 18: Now open app's 'build.gradle' file and add this code in it.




Step 19: Clean project.






package com.example.hellotolivewallpaper;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITiledTextureRegion;

import java.io.IOException;

public class HelloToLiveWallpaperService extends BaseLiveWallpaperService implements IOnSceneTouchListener
{
    private static final String TAG = "HelloTo";

    private static float CAMERA_WIDTH = 480;
    private static float CAMERA_HEIGHT = 800;

    private Scene mScene;
    private ITiledTextureRegion mHelloTR;
    private AnimatedSprite mHelloSprite;

    @Override
    public EngineOptions onCreateEngineOptions()
    {
        try
        {
            final DisplayMetrics displayMetrics = new DisplayMetrics();
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(displayMetrics);
            wm.getDefaultDisplay().getRotation();
            CAMERA_WIDTH = displayMetrics.widthPixels;
            CAMERA_HEIGHT = displayMetrics.heightPixels;
        } catch (Exception e)
        {
            Log.e(TAG, "onCreateEngineOptions " + e.toString());
        }

        //create a camera
        Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        //engine options
        return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
    {
        //create texture
        BitmapTextureAtlas textureAtlas = new BitmapTextureAtlas(getTextureManager(), 600, 150, TextureOptions.BILINEAR);
        this.mHelloTR = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textureAtlas, getAssets(), "gfx/text_hello.png", 0, 0, 2, 1);
        this.getEngine().getTextureManager().loadTexture(textureAtlas);

        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
    {
        //create a scene
        mScene = new Scene();

        //create a black background
        mScene.setBackground(new Background(0.0f, 0.0f, 0.0f));

        //create a sprite
        mHelloSprite = new AnimatedSprite(0.5f * CAMERA_WIDTH, 0.5f * CAMERA_HEIGHT, mHelloTR, this.getVertexBufferObjectManager());

        //attach our sprite to the scene
        mScene.attachChild(mHelloSprite);

        //register scene touch listener
        mScene.setOnSceneTouchListener(this);

        pOnCreateSceneCallback.onCreateSceneFinished(mScene);
    }

    @Override
    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException
    {
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
    {
        //handling scene on touch event
        if (pScene == mScene)
        {
            switch (mHelloSprite.getCurrentTileIndex())
            {
                case 0:
                    mHelloSprite.setCurrentTileIndex(1);
                    break;

                case 1:
                    mHelloSprite.setCurrentTileIndex(0);
                    break;
            }
        }

        return false;
    }
}
Step 20: Create new java file 'HelloToLiveWallpaperService.java' and just paste this code.






Step 21: Create new directory 'assets' in app>main>




Step 22: Create new directory 'gfx' in app>main>assets>






Step 23: Add this 'text_hello.png' image in app>main>assets>gfx>




 <!-- Live Wallpaper Service -->
        <service
            android:name=".HelloToLiveWallpaperService"
            android:description="@string/wallpaper_description"
            android:enabled="true"
            android:icon="@mipmap/ic_launcher"
            android:permission="android.permission.BIND_WALLPAPER" >
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>

            <meta-data
                android:name="android.service.wallpaper"
                android:resource="@xml/wallpaper" />
        </service>
Step 24: Open 'AndroidManifest.xml' and this code before 'application ending tag'.




package com.example.hellotolivewallpaper;

import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);

        Intent intent;

        // try the new Jelly Bean direct android wallpaper chooser first
        try
        {
            ComponentName component = new ComponentName(getPackageName(), getPackageName() + ".HelloToLiveWallpaperService");
            intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
            intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, component);
            startActivity(intent);
        } catch (android.content.ActivityNotFoundException e3)
        {
            // try the generic android wallpaper chooser next
            try
            {
                intent = new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
                startActivity(intent);
            } catch (android.content.ActivityNotFoundException e2)
            {
                // that failed, let's try the nook intent
                try
                {
                    intent = new Intent();
                    intent.setAction("com.bn.nook.CHANGE_WALLPAPER");
                    startActivity(intent);
                } catch (android.content.ActivityNotFoundException e)
                {
                    // everything failed, let's notify the user
                    Toast.makeText(MainActivity.this, "Wallpaper chooser not found", Toast.LENGTH_SHORT).show();
                }
            }
        }
        finish();
    }
}
Step 25: Finally open app>main>java>com>example>hellotolivewallpaper>MainActivity.java and paste this code in it. That's it.

Run the app...
Output:



Full source code(rar formate) - Download
HAPPY CODING...



Search Tags: how to make live wallpaper in android studio live wallpaper for android apk create live wallpaper for android studio android live wallpaper android studio how to create live wallpaper for android using android studio Create a Live Wallpaper on Android Studio How to create a Live Wallpaper in Android how to make live wallpaper for android phones how to make a live wallpaper for android tutorial Create Your Own Android Live Wallpaper using AndEngine How to create a live wallpaper app in Android Studio How to Use OpenGL ES 2 in an Android Live Wallpaper