Andengine Sprite Relative Rotation

  1: package com.example.andenginetest;
  2: 
  3: import org.andengine.engine.camera.Camera;
  4: import org.andengine.engine.options.EngineOptions;
  5: import org.andengine.engine.options.ScreenOrientation;
  6: import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
  7: import org.andengine.entity.scene.IOnSceneTouchListener;
  8: import org.andengine.entity.scene.Scene;
  9: import org.andengine.entity.sprite.Sprite;
 10: import org.andengine.input.touch.TouchEvent;
 11: import org.andengine.opengl.texture.TextureOptions;
 12: import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
 13: import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
 14: import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
 15: import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
 16: import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
 17: import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
 18: import org.andengine.opengl.texture.region.ITextureRegion;
 19: import org.andengine.ui.activity.BaseGameActivity;
 20: import org.andengine.util.math.MathUtils;
 21: 
 22: public class MainActivity extends BaseGameActivity implements IOnSceneTouchListener {
 23: 
 24: 	// ====================================================
 25: 	// CONSTANTS
 26: 	// ====================================================
 27: 	public static final int WIDTH = 800;
 28: 	public static final int HEIGHT = 480;
 29: 	public static final int SPRITE_DEFAULT_ROTATION = -90;
 30: 
 31: 	// ====================================================
 32: 	// VARIABLES
 33: 	// ====================================================
 34: 	private Scene mScene;
 35: 	private Camera mCamera;
 36: 
 37: 	private Sprite mArrowSprite;
 38: 	private Sprite mMarbleSprite;
 39: 
 40: 	private ITextureRegion mArrowTextureRegion;
 41: 	private ITextureRegion mMarbleTextureRegion;
 42: 
 43: 	// ====================================================
 44: 	// CREATE ENGINE OPTIONS
 45: 	// ====================================================
 46: 	@Override
 47: 	public EngineOptions onCreateEngineOptions() {
 48: 		mCamera = new Camera(0, 0, WIDTH, HEIGHT);
 49: 
 50: 		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
 51: 
 52: 		return engineOptions;
 53: 	}
 54: 
 55: 	// ====================================================
 56: 	// CREATE RESOURCES
 57: 	// ====================================================
 58: 	@Override
 59: 	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) {
 60: 		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
 61: 
 62: 		BuildableBitmapTextureAtlas mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
 63: 
 64: 		mArrowTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "arrow.png");
 65: 		mMarbleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "marble.png");
 66: 
 67: 		try {
 68: 			mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 1));
 69: 			mBitmapTextureAtlas.load();
 70: 		} catch (TextureAtlasBuilderException e) {
 71: 			e.printStackTrace();
 72: 		}
 73: 
 74: 		pOnCreateResourcesCallback.onCreateResourcesFinished();
 75: 	}
 76: 
 77: 	// ====================================================
 78: 	// CREATE SCENE
 79: 	// ====================================================
 80: 	@Override
 81: 	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
 82: 		mScene = new Scene();
 83: 
 84: 		// Allow our scene to register this activities touch events
 85: 		mScene.setOnSceneTouchListener(this);
 86: 
 87: 		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
 88: 	}
 89: 
 90: 	// ====================================================
 91: 	// POPULATE SCENE
 92: 	// ====================================================
 93: 	@Override
 94: 	public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) {
 95: 
 96: 		// Create our marble sprite in the top left corner of the screen
 97: 		// initially.
 98: 		mMarbleSprite = new Sprite(0, 0, mMarbleTextureRegion, mEngine.getVertexBufferObjectManager());
 99: 		mScene.attachChild(mMarbleSprite);
100: 
101: 		// Set our arrow sprite's position to the center of the screen
102: 		final float x = (WIDTH / 2) - (mArrowTextureRegion.getWidth() / 2);
103: 		final float y = (HEIGHT / 2) - (mArrowTextureRegion.getHeight() / 2);
104: 
105: 		// Create and attach our arrow sprite to the scene
106: 		mArrowSprite = new Sprite(x, y, mArrowTextureRegion, mEngine.getVertexBufferObjectManager());
107: 		mScene.attachChild(mArrowSprite);
108: 
109: 		pOnPopulateSceneCallback.onPopulateSceneFinished();
110: 	}
111: 
112: 	// We can override the onSceneTouchEvent() method since our activity
113: 	// now implements the IOnSceneTouchListener
114: 	@Override
115: 	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
116: 		// If a user moves their finger on the device
117: 		if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE) {
118: 
119: 			// Set our marble's position to the touched area on the screen
120: 			mMarbleSprite.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());			
121: 
122: 			final float dX = (mArrowSprite.getX() - mMarbleSprite.getX());
123: 			final float dY = (mArrowSprite.getY() - mMarbleSprite.getY());
124: 
125: 			// We can use the atan2 function to find the angle
126: 			// Additionally, OpenGL works with degrees so we must convert
127: 			// from radians
128: 			final float rotation = MathUtils.radToDeg((float) Math.atan2(-dY, dX)) + SPRITE_DEFAULT_ROTATION;
129: 
130: 			// Set the new rotation for the arrow
131: 			mArrowSprite.setRotation(rotation);
132: 			return true;
133: 		}
134: 		return false;
135: 	}
136: 
137: }
138: 

Serialize, De-serialize Android SDcard

public static void writeObject(String filePath, DamageAssessmentFormPojo pojo) throws IOException {

            filePath += DefaultReportName;
            FileOutputStream fos = new FileOutputStream(filePath);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(pojo);

        }

        public static Object readObject(String filePath) throws Exception {
            filePath += DefaultReportName;
            FileInputStream fis = new FileInputStream(filePath);
            ObjectInputStream ois = new ObjectInputStream(fis);
            return (ois.readObject());
        }

Android Super Fast Emulator

Blazing fast android emulator-x86 on ICS

In this article I am going to show android developers how Intel’s hypervisor with Intel Linux 2.6 kernel outperforms native ARM hardware performance.

First download “Intel Atom System Image”

Next, to create a new AVD we perform the following steps:

  1. Click on "Tools", "Manage AVDs" and "New.."
  2. Name the AVD "x86_4_0_3"
  3. Select "Android 4.0.3 - API Level 15" as Target
  4. Choose "Intel Atom (x86)" as CPU/ABI
  5. Click "Create AVD"

Tags:

Android fast emulator x86 intel atom hypervision

get height width or inflated layout

Hi,

its simple and easy :)

public Bitmap getBitmapFromView(RelativeLayout v) {
v.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.RGB_565);

Canvas c = new Canvas(b);
v.draw(c);
return b;
}

Covert Layout into Bitmap using inflator

Hi,

I this Tutorial I am going to demonstrate you how to convert your layouts into images and then merge those separate images into one image :)

  1: 
  2: package com.az.image.merging;
  3: 
  4: import java.io.File;
  5: import java.io.FileNotFoundException;
  6: import java.io.FileOutputStream;
  7: import java.io.IOException;
  8: 
  9: import android.app.Activity;
 10: import android.graphics.Bitmap;
 11: import android.graphics.Bitmap.CompressFormat;
 12: import android.graphics.BitmapFactory;
 13: import android.graphics.Canvas;
 14: import android.graphics.drawable.BitmapDrawable;
 15: import android.os.Bundle;
 16: import android.os.Environment;
 17: import android.util.Log;
 18: import android.view.KeyEvent;
 19: import android.view.LayoutInflater;
 20: import android.view.View;
 21: import android.view.View.MeasureSpec;
 22: import android.view.ViewGroup.LayoutParams;
 23: import android.widget.Button;
 24: import android.widget.RelativeLayout;
 25: import android.widget.Toast;
 26: 
 27: public class MergeImages extends Activity {
 28: 	protected static final String TAG = MergeImages.class.getName();
 29: 	Bitmap mBackImage, mTopImage, mBackground, mInnerImage, mNewSaving;
 30: 	Canvas mComboImage;
 31: 	FileOutputStream mFileOutputStream;
 32: 	BitmapDrawable mBitmapDrawable;
 33: 	private static String mTempDir;
 34: 	private String mCurrent = null;
 35: 	private Button mSave = null;
 36: 
 37: 	private final int WIDTH = 700;
 38: 	private final int HEIGHT = 415;
 39: 	private final float PADDING_TOP = 10F;
 40: 	private final float PADDING_RIGHT = 10F;
 41: 
 42: 	@Override
 43: 	public void onCreate(Bundle savedInstanceState) {
 44: 		super.onCreate(savedInstanceState);
 45: 		setContentView(R.layout.main);
 46: 
 47: 		mTempDir = Environment.getExternalStorageDirectory() + "/" + "AZ" + "/";
 48: 		mCurrent = "AZ_combile_image.png";
 49: 		prepareDirectory();
 50: 
 51: 		mBackground = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
 52: 		mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.map); 
 53: 
 54: Inflating Image:
 55: 		LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
 56: 		RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.layout_right, null);
 57: 
 58: 		mTopImage = getBitmapFromView(view);
 59: 		
 60: 		mInnerImage = BitmapFactory.decodeResource(getResources(), R.drawable.logo); 
 61: 
 62: 		mComboImage = new Canvas(mBackground);
 63: 		mComboImage.drawBitmap(mBackImage, 0f, 0f, null);
 64: 		mComboImage.drawBitmap(mTopImage, WIDTH - (mTopImage.getWidth() + PADDING_RIGHT), PADDING_TOP, null);
 65: 		mComboImage.drawBitmap(mInnerImage, WIDTH - (mInnerImage.getWidth() + PADDING_RIGHT), mTopImage.getHeight() + PADDING_TOP, null);
 66: 		mFileOutputStream = null;
 67: 
 68: 		mSave = (Button) findViewById(R.id.btn_save);
 69: 		mSave.setOnClickListener(new View.OnClickListener() {
 70: 			public void onClick(View v) {
 71: 				Log.v(TAG, "Save Tab Clicked");
 72: 				try {
 73: 					mBitmapDrawable = new BitmapDrawable(mBackground);
 74: 					mNewSaving = ((BitmapDrawable) mBitmapDrawable).getBitmap();
 75: 					String FtoSave = mTempDir + mCurrent;
 76: 					File mFile = new File(FtoSave);
 77: 					mFileOutputStream = new FileOutputStream(mFile);
 78: 					mNewSaving.compress(CompressFormat.PNG, 95, mFileOutputStream);
 79: 					mFileOutputStream.flush();
 80: 					mFileOutputStream.close();
 81: 				} catch (FileNotFoundException e) {
 82: 					Log.v(TAG, "FileNotFoundExceptionError " + e.toString());
 83: 				} catch (IOException e) {
 84: 					Log.v(TAG, "IOExceptionError " + e.toString());
 85: 				}
 86: 			}
 87: 		});
 88: 
 89: 	}// onCreate
 90: 
 91: 	private boolean prepareDirectory() {
 92: 		try {
 93: 			if (makeDirectory()) {
 94: 				return true;
 95: 			} else {
 96: 				return false;
 97: 			}
 98: 		} catch (Exception e) {
 99: 			e.printStackTrace();
100: 			Toast.makeText(this, "SD Card Error", 1000).show();
101: 			return false;
102: 		}
103: 	}
104: 
105: 	private boolean makeDirectory() {
106: 		File mTempFile = new File(mTempDir);
107: 		if (!mTempFile.exists()) {
108: 			mTempFile.mkdirs();
109: 		}
110: 
111: 		if (mTempFile.isDirectory()) {
112: 			File[] mFiles = mTempFile.listFiles();
113: 			for (File mEveryFile : mFiles) {
114: 				if (!mEveryFile.delete()) {
115: 					System.out.println("Failed to delete" + mEveryFile);
116: 				}
117: 			}
118: 		}
119: 		return (mTempFile.isDirectory());
120: 	}
121: 
122: 	@Override
123: 	public boolean onKeyDown(int keyCode, KeyEvent event) {
124: 		if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)) {
125: 			onBackPressed();
126: 		}
127: 		return super.onKeyDown(keyCode, event);
128: 	}
129: 
130: 	public void onBackPressed() {
131: 		finish();
132: 	}
133: 
134: 	public Bitmap getBitmapFromView(RelativeLayout v) {
135: /**
136: Very Important Point
137: Getting inflated layout width or height return 0 because it haven't been attached into any view
138: so you need to call view.measure( ) and calling measure will throw Null Pointer Exception because inflator 
139: doesn't read width and height properties of layout so we need to porive it thorse.
140: */
141: 		v.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
142: 		v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
143: 		v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());		
144: 		Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.RGB_565);
145: 
146: 		Canvas c = new Canvas(b);
147: 		v.draw(c);
148: 		return b;
149: 	}
150: 	
151: