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...


2 comments:

  1. Discover the real cost of mobile app development in Australia for 2025. Learn what factors impact pricing, how to budget smartly, and ways to maximise value whether you're a startup or enterprise. Stay ahead in the app game with expert insights from Sunrise Technologies.
    app development cost Australia

    ReplyDelete
  2. Exceptional achievements! It's clear your company understands both the potential and responsibility of AI. Looking forward to seeing how your AI-driven applications continue to transform industries.
    Best AI Application Development Company

    ReplyDelete