Thứ tư, ngày 20 tháng 9 năm 2017

AndEngine - Cách làm Livewallpaper

Ngày đăng: 24/8/2012, 8:42:41PM | Lượt xem: 2,894
Hot!
tag ,

 

 1. Tạo project android Bubbles có activity là BubblesActivity, add các gói andengine.jar, andenginelivewallpaper.jar, andenginephysicbox2dextension.jar

(trong file mình gửi sẽ có các gói này)
 
2. Tạo 1 class Settings.java, 
 
3. Trong thư mục res/xml, tạo 2 file wallpaper.xml và wallpaper_settings.xml
wallpaper.xml: sẽ include activity của class Settings
<?xml version="1.0" encoding="UTF-8"?>
<wallpaper
        xmlns:android="http://schemas.android.com/apk/res/android"  
        android:thumbnail="@drawable/icon"
        android:settingsActivity="com.live.Settings"/>
wallpaper_settings.xml chứa 1 checkbox, nếu background dc check sẽ hiện ra, ko check sẽ ko hiện background. 
<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen
		xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="Settings"
        >
        <CheckBoxPreference android:key="pref_background" android:defaultValue="true" android:title="Background">
        </CheckBoxPreference>
        
</PreferenceScreen>
4. Edit file manifest.xml, sử dụng <uses-feature android:name="android.software.live_wallpaper" />, và khai báo các service livewallpaper và activity Setting
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.live"
      android:versionCode="1"
      android:versionName="1.0">
    
    <uses-sdk android:minSdkVersion="7" />
    
    <uses-feature android:name="android.software.live_wallpaper" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    	<service
            android:name=".BubblesActivity"
			android:enabled="true"
			android:icon="@drawable/icon"
			android:label="@string/app_name"
			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>
       
       <activity
        	android:label="@string/wallpaper_settings"
        	android:name=".Settings"
        	android:theme="@android:style/Theme.Light.WallpaperSettings"
        	android:exported="true">
        </activity>

    </application>
</manifest>
5. Tạo class Ball.java , kế thừa lớp Sprite, cài đặt thông số fixtureDef là trọng lượng, độ đàn hồi và ma sát
Ở hàm onManageUpdate , sẽ luôn cho nó hiển thị ở 1 vị trí cố định, tại vì khi va chạm thì nó sẽ xoay theo hướng va chạm, nhìn ko thật tế, nên mình gọi mBody.setTransform(mBody.getPosition().x, mBody.getPosition().y, 0); với 0 là giá trị góc xoay.
package com.live;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class Ball extends Sprite {
	
	public Body mBody;
	
	public Ball(float pX, float pY, float pTileWidth, float pTileHeight,
			TextureRegion pTextureRegion) {
		super(pX, pY, pTileWidth, pTileHeight, pTextureRegion);
		setPhysicsWorld();
	}
	
	public void setPhysicsWorld() {
		FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(0.005f, 1.005f, 0.085f);;
		mBody = PhysicsFactory.createCircleBody(BubblesActivity.mfsw, this, BodyType.DynamicBody, fixtureDef);
		BubblesActivity.mfsw.registerPhysicsConnector(new PhysicsConnector(this, mBody));
	}

	@Override
	public void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		mBody.setTransform(mBody.getPosition().x, mBody.getPosition().y, 0);
	}
}
6. File BublesActivity.java, có 8 trái bóng
Hàm onLoadScene, chắc cũng quen thuộc với các bạn rồi, trong đây mình khai báo thêm các cái shape để làm viền, ko cho trái bóng ra khỏi
Hàm getWind, dùng để tạo 1 cơn gió thổi (sau 1 khoản thời gian nhất định) làm các trái bóng bay lên, tránh trường hợp trái bóng đứng yên
Hàm onConfigurationChanged dùng để xử lý khi xoay màn hình ngang
package com.live;

import java.util.ArrayList;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.util.Vector2Pool;
import org.anddev.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;
import org.anddev.andengine.util.MathUtils;

import android.content.res.Configuration;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class BubblesActivity extends BaseLiveWallpaperService implements IAccelerometerListener {
	// ===========================================================
	// Constants
	// ===========================================================
	
	public static final String SHARED_PREFS_NAME = "livewallpapertemplatesettings";

	private static int CAMERA_WIDTH = 480;
	private static int CAMERA_HEIGHT = 800;
	
	private float GRAVITY = 0.78f;
	
	private ScreenOrientation mInitOrientation = ScreenOrientation.PORTRAIT;
	
	public static FixedStepPhysicsWorld mfsw;
	public static Scene scene;
	
	private Vector2 wind = new Vector2(1,-10);

	// ===========================================================
	// Fields
	// ===========================================================
	public static BitmapTextureAtlas tt_ball;
	public static TextureRegion tr_ball;
	
	public static BitmapTextureAtlas tt_bar;
	public static TextureRegion tr_bar;
	
	private BitmapTextureAtlas tt_bg;
	private TextureRegion tr_bg;
	private static SpriteBackground mBackground;
	
	//Shared Preferences
	//private SharedPreferences mSharedPreferences;
	
	private final int MAX_BALL = 8;
	private final int BALL_SIZE=140;
	
	private ArrayList<Ball> arrBall = new ArrayList<Ball>();
	private int count = 0;

	@Override
	public org.anddev.andengine.engine.Engine onLoadEngine()
	{
		Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions eo = new EngineOptions(true, mInitOrientation, new FillResolutionPolicy(), mCamera);
		eo.getTouchOptions().setRunOnUpdateThread(true);
		eo.getRenderOptions().disableExtensionVertexBufferObjects();
		return new org.anddev.andengine.engine.Engine(eo);
	}

	@Override
	public void onLoadResources()
	{
		tt_ball = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		tr_ball = BitmapTextureAtlasTextureRegionFactory.createFromAsset(tt_ball, this, "gfx/bubble.png", 0, 0);

		tt_bg = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		tr_bg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(tt_bg, this, "gfx/bg.jpg", 0, 0);
		
		this.getEngine().getTextureManager().loadTexture(tt_ball);
		this.getEngine().getTextureManager().loadTexture(tt_bg);
	}

	@Override
	public Scene onLoadScene()
	{
		scene = new Scene();
		
		mBackground = new SpriteBackground(new Sprite(0, 0,CAMERA_WIDTH,CAMERA_HEIGHT, tr_bg));
		scene.setBackground(mBackground);
		
		// fixed step physic world
		mfsw = new FixedStepPhysicsWorld(30, new Vector2(0,GRAVITY), false);

		final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
        final Shape roof = new Rectangle(0, -30, CAMERA_WIDTH, 2);
        final Shape left = new Rectangle(-30, 0, 2, CAMERA_HEIGHT);
        final Shape right = new Rectangle(CAMERA_WIDTH + 30, 0, 2, CAMERA_HEIGHT);
        
        final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0.005f, 0.9f, 0.1f);
        PhysicsFactory.createBoxBody(mfsw, ground, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(mfsw, roof, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(mfsw, left, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(mfsw, right, BodyType.StaticBody, wallFixtureDef);

        scene.attachChild(ground);
		scene.attachChild(roof);
		scene.attachChild(left);
		scene.attachChild(right);
		
		for (int i=0; i < MAX_BALL; i++) {
			int x = MathUtils.random(0, CAMERA_WIDTH-BALL_SIZE);
			int y = BALL_SIZE;
			final Ball ball = new Ball(x, y, BALL_SIZE, BALL_SIZE, tr_ball);
			arrBall.add(ball);
			scene.attachChild(ball);
		}
		
		scene.registerUpdateHandler(mfsw);
		
		scene.registerUpdateHandler(getWind());
		return scene;
	}
	
	public static void displayBackground(boolean b) {
		if (b) {
			scene.setBackground(mBackground);
		} else {
			scene.setBackground(new ColorBackground(0, 0, 0));
		}
	}
	
	@Override
	public void onLoadComplete()
	{

	}
	
	@Override
	protected void onTap(final int pX, final int pY)
	{
		
	}
	

	@Override
	public void onPauseGame() {
		
	}

	@Override
	public void onResumeGame() {
		
	}

	@Override
	public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
		final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), GRAVITY);
        mfsw.setGravity(gravity);
        //Vector2Pool.recycle(gravity);
	}

	@Override
	public void onConfigurationChanged (Configuration newConfig){
		super.onConfigurationChanged(newConfig);
		if (mInitOrientation == ScreenOrientation.PORTRAIT) {
			if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
				scene.setScaleX((float)CAMERA_WIDTH / (float)CAMERA_HEIGHT);
				scene.setScaleY((float)CAMERA_HEIGHT / (float)CAMERA_WIDTH);
	        }
	        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
	        	scene.setScale(1);
	        }
		}
		if (mInitOrientation == ScreenOrientation.LANDSCAPE) {
            if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            	scene.setScale(1);
            }
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            	scene.setScaleX((float)CAMERA_WIDTH / (float)CAMERA_HEIGHT);
            	scene.setScaleY((float)CAMERA_HEIGHT / (float)CAMERA_WIDTH);
            }
	    }
		// updateBackground();
		// update bottom & right;
	}
	
	private IUpdateHandler getWind() {
		return new IUpdateHandler() {

			@Override
			public void onUpdate(float pSecondsElapsed) {
				count = count + 1;
				if (count > 400) {
					for (Ball b : arrBall) {
						b.mBody.applyForce(wind, b.mBody.getWorldCenter());
					}
					count = 0;
				}
			}

			@Override
			public void reset() {
				
			}
			
		};
	}
}
7. File Settings.java
Hàm onSharedPreferenceChanged sẽ đc gọi khi user thay đổi settings.
package com.live;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
{	
	@Override
    protected void onCreate(Bundle icicle)
	{
        super.onCreate(icicle);
        getPreferenceManager().setSharedPreferencesName(BubblesActivity.SHARED_PREFS_NAME);
        addPreferencesFromResource(R.xml.wallpaper_settings);
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onDestroy()
    {
        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        super.onDestroy();
    }

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
	{
		boolean isBG = sharedPreferences.getBoolean("pref_background", true);
		BubblesActivity.displayBackground(isBG);
	}
}
8. Run và chạy, kết quả như hình đính kèm. Hi vọng giúp ích được cho mọi người.
 
theo vietandroid
 Chia sẻ qua: 
Hot!
Ý kiến bạn đọc

These items will be permanently deleted and cannot be recovered. Are you sure?

Gallery

image

Maecenas viverra rutrum pulvinar

Maecenas viverra rutrum pulvinar! Aenean vehicula nulla sit amet metus aliquam et malesuada risus aliquet. Vestibulum rhoncus, dolor sit amet venenatis porta, metus purus sagittis nisl, sodales volutpat elit lorem…

Read more

Text Links

Thiết kế logo chuyên nghiệp Insky
DAFABET
W88 w88b.com/dang-ky-tai-khoan-w88
W88
ca do bong da online
Copyright © 2011 - 2012 vietshare.vn by phamkhuong102@gmail.com doanhkisi2315@gmail.com. All rights reserved.