Griffoid
Wednesday, September 8, 2010
Using arraylists properly with android
Late post so ill keep it short.
Android development hinges on being efficent with memory. Declaring objects in a loop is not efficient. If you use a foreach loop with an arraylist, you are creating an iterator each time you run through the loop. Instead, loop through your arraylist using a for loop like this:
for(I =0; I<mylist.size();I++){
Myobject=mylist.get(I);
...
)
That will prevent memory leaks and keep your game or app running smoothley since the garbage collector will be swift. See my other post for how to track all memory leaks while developing with android.
Tuesday, September 7, 2010
How to Find Memory Leaks in Android Applications
While working on a game, I noticed the Garbage Collector was causing significant delays while cleaning up memory. The GC is used by Java to free up memory that has been taken by objects that your program can no longer access. Since a game is inherently developed in a loop, it is very easy to create unnecessary objects over and over. Once these take up too much memory, the GC must run to free up more memory for the App. Unfortunately this can cause a pause in your game, or even a simple app.
The android SDK comes with a great tool to find these memory leaks.
-Just go to your tools directory under your SDK folder, and open the ddms. Make sure you don't already have eclipse running, as only one debugger can attach at a time.
-Run your application on your attached device (your phone)
-Select your application in the application list on the left side of the ddms
-Select the "allocation tracker" tab in the ddms
-click "Start Tracking" in the allocation tracker tab
-Interact with your application on your device for a while
-click "Get Allocations" in the ddms
This will list the memory allications, in descending size. You can click on each allocation and the full stack trace is given, which allows you to determine what line of what class the allocation was made. Very easy! Now go to that line and smack yourself in the forehead for that worthless declaration, and become a greener programmer!
This will make your game/app run much smoother. However, remember that the garbage collector normally runs, so you can never get rid of all GC collections.
original post: Tracking Memory Leaks
The android SDK comes with a great tool to find these memory leaks.
-Just go to your tools directory under your SDK folder, and open the ddms. Make sure you don't already have eclipse running, as only one debugger can attach at a time.
-Run your application on your attached device (your phone)
-Select your application in the application list on the left side of the ddms
-Select the "allocation tracker" tab in the ddms
-click "Start Tracking" in the allocation tracker tab
-Interact with your application on your device for a while
-click "Get Allocations" in the ddms
This will list the memory allications, in descending size. You can click on each allocation and the full stack trace is given, which allows you to determine what line of what class the allocation was made. Very easy! Now go to that line and smack yourself in the forehead for that worthless declaration, and become a greener programmer!
This will make your game/app run much smoother. However, remember that the garbage collector normally runs, so you can never get rid of all GC collections.
original post: Tracking Memory Leaks
Thursday, September 2, 2010
Use Keyboard and Touch Screen Input in Android Game
Just a few things you need here, but this can be really frustrating if you don't know what's happening:
1.You must keep your view in focus when android enters touch mode.
2.Your view must be in focus.
3.You must have an onkeydown listener of some sort.
So to accomplish these, in your View constructor:
1.this.setFocusable(true); //allow the view to be focused
this.setFocusableInTouchMode(true); // allow the view to be focused while in touch mode
2. this.requestFocus(); //set the focus on this view
Then in your View class:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d("-------------------->code", Integer.toString(keyCode));
if (keyCode == KeyEvent.KEYCODE_FOCUS) {
//handle focus button pressed
return true;
}
return false;
}
Returning true tells android you have consumed the keypress. Returning false passes it along to other listeners.
1.You must keep your view in focus when android enters touch mode.
2.Your view must be in focus.
3.You must have an onkeydown listener of some sort.
So to accomplish these, in your View constructor:
1.this.setFocusable(true); //allow the view to be focused
this.setFocusableInTouchMode(true); // allow the view to be focused while in touch mode
2. this.requestFocus(); //set the focus on this view
Then in your View class:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d("-------------------->code", Integer.toString(keyCode));
if (keyCode == KeyEvent.KEYCODE_FOCUS) {
//handle focus button pressed
return true;
}
return false;
}
Returning true tells android you have consumed the keypress. Returning false passes it along to other listeners.
Wednesday, September 1, 2010
Angry Birds Coming to Android Friday
Since so many people signed up to be beta testers, the developers of Angry Birds have decided to release the beta version via the Android Market this Friday.
For those who don't know, Angry Birds is the popular iPhone game in which you fling exploding birds into buildings. It has sold millions of copies.
Original post: droid-life
For those who don't know, Angry Birds is the popular iPhone game in which you fling exploding birds into buildings. It has sold millions of copies.
Original post: droid-life
Monday, August 30, 2010
Force Landscape Mode/Orientation Android Development
The best way to do this is in your AndroidManifest.xml file. Just right click and open with the text editor in eclipse. Then add:
android:screenOrientation="landscape"
to your the activity that you want to be in landscape mode. Here's what my androidManifest.xml file looks like, sorry for the poor formatting options on Blogger:
android:screenOrientation="landscape"
to your the activity that you want to be in landscape mode. Here's what my androidManifest.xml file looks like, sorry for the poor formatting options on Blogger:
How to Debug via USB Android
This is very easy if you are already setup with adb in eclipse:
1.plug in your device(phone)
2.enable USB debugging on device
3.debug/launch app from eclipse as normal, it will autoselect your device if it is attached.
If logcat isn't working, close and reopen eclipse.
1.plug in your device(phone)
2.enable USB debugging on device
3.debug/launch app from eclipse as normal, it will autoselect your device if it is attached.
If logcat isn't working, close and reopen eclipse.
Handle Multitouch Android
Here's the basics you need to know:
- setup an event handler to handle your ontouch events
- touch events have different actions: ACTION_DOWN, ACTION_UP, etc. are used for the first touch event registered. ACTION_POINTER_DOWN, ACTION_POINTER_UP are used for the second touch event. As far as I know Android only supports 2 touch events.
- to use the second event, you must combine the event.getAction() and MotionEvent.ACTION_MASK by anding them as seen below.
First you need to catch your action in your touch event and store it as an int:
public class GameEvent implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
actionCode = event.getAction() & MotionEvent.ACTION_MASK;//this combines single and multitouch
}
}
Then test to see what the event action was:
if(actionCode == MotionEvent.ACTION_DOWN){
}else if(actionCode == MotionEvent.ACTION_POINTER_DOWN )...
from here you can get the X&Y coordinates of each touch, the pressure, and more.
- setup an event handler to handle your ontouch events
- touch events have different actions: ACTION_DOWN, ACTION_UP, etc. are used for the first touch event registered. ACTION_POINTER_DOWN, ACTION_POINTER_UP are used for the second touch event. As far as I know Android only supports 2 touch events.
- to use the second event, you must combine the event.getAction() and MotionEvent.ACTION_MASK by anding them as seen below.
First you need to catch your action in your touch event and store it as an int:
public class GameEvent implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
actionCode = event.getAction() & MotionEvent.ACTION_MASK;//this combines single and multitouch
}
}
Then test to see what the event action was:
if(actionCode == MotionEvent.ACTION_DOWN){
}else if(actionCode == MotionEvent.ACTION_POINTER_DOWN )...
from here you can get the X&Y coordinates of each touch, the pressure, and more.
Subscribe to:
Posts (Atom)