miércoles, 5 de mayo de 2010

Snippet: Short an URL using http://is.gd/

public String getShortUrl(String urlToShort){
String apiUrl = "http://is.gd/api.php?longurl=" + urlToShort;
String ret = "";

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(apiUrl));
InputStream is = response.getEntity().getContent();

if (is != null) {
StringBuilder sb = new StringBuilder();
String line;

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} finally {
is.close();
}
ret = sb.toString();

}
} catch (ClientProtocolException e) {
Log.e("Error", "Error: " + e.getMessage());
} catch (IOException e) {
Log.e("Error", "Error: " + e.getMessage());
}
return ret;
}

miércoles, 7 de abril de 2010

Huuuge crashing on Eclipse when refreshing or compiling an Android Project...

If you suffer someday for any reason this error:
[2009-03-30 17:28:14 - Dex Loader] Unable to execute dex: null
[2009-03-30 17:28:14 - LlianeJapan]
Conversion to Dalvik format failed: Unable to execute dex: nu
Here is the solution, just adding two lines to the eclipse.init file...
http://code.google.com/p/android/issues/detail?id=2328

martes, 16 de marzo de 2010

Difference of px, dp, dip and sp in android..

px
Pixels - corresponds to actual pixels on the screen.

in
Inches - based on the physical size of the screen.

mm
Millimeters - based on the physical size of the screen.

pt
Points - 1/72 of an inch based on the physical size of the screen.

dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

Integra el NDK en Eclipse

Para el desarrollo de juegos y aplicaciones 3d podras notar que necesitas un plus mas de velocidad, ahi es donde entra el NDK de Android, notaras que aplicaciones hechas en C con la NDK van de 10 a 100 veces mas rapido que en Java, el problema lo tenemos al desarrollar. El proceso para compilar una aplicacion usando la NDK es lento y tedioso, pero eso era hasta hoy, vamos a enseñar una forma de integrar el NDK en Eclipse, para los que trabajen mucho con la NDK lo agradeceran.

Para esto vamos a necesitar basicamente los siguientes componentes:

Android NDK (Native Development Kit)
Eclipse CDT (C Development Tooling)
Y si estamos en Windows, Cygwin Con los paquetes de devel instalados, especialmente GCC y Make.

Instalamos las 3 cosas, como recomendacion instalar el NDK en c:\Android_NDK ya que los proyectos que la usen tiene que estar en una carpeta llamada apps dentro de c:\Android_NDK.

Para probar que hemos instalado bien el Cygwin y la NDK podemos hacer los siguiente:

Ejecutar cygwin
cd /cygdrive/c/Android_NDK
make APP=hello-jni


Esto deberia compilar sin errores, en caso contrario revisa que has instalado todos los paquetes dev del gcc y make en el Cygwin ejecutando de nuevo su setup.exe

Ahora empieza lo bueno

Si ya has programado con la NDK, probablemente hayas usado para editar los ficheros C/CPP el vim o cualquier otro editor de textos, ahora con el CDT en Eclipse, no volveras a tener que usar otra cosa que no sea Eclipse. Luego de editar los archivos tendrias que ejecutar un make APP=myapp cada vez que quieras hacer un build, luego cliquear en refresh en el Eclipse a que el archivo .so generado aparezca en el proyecto. Esto como veis es "un fracaso absoluto" hay una manera mucho mas eficaz como veremos ahora.

¿Habeis usado alguna vez builders en Eclipse? Son como una especie de triggers que se pueden configurar para que ejecuten y refresquen lo que les digas por ti. Asi es como se configuran:

Boton derecho en tu proyecto, click en propiedades.
Selecciona la opcion de Builders en la lista de la izquierda.
Clickea en New en la parte derecha.
Selecciona Program como configuration type.
Ponle el nombre que quieras.
Location
- c:\cygwin\bin\bash.exe
Working Directory - c:\cygwin\bin
Arguments -
--login -c "cd /cygdrive/c/Android_NDK && make APP=myapp"
Estate seguro de los dos guiones antes de login y de las comillas despues de -c
Ahora ve a la pestaña Refresh
Marca "Refresh resources upon completion"
Selecciona "Specific resources"
Clickea en el boton "Specify resources" y selecciona el directorio lib de tu proyecto.
Marca "Recursively include sub-folders"
Ahora ve a la pestaña build options.
Marca "Allocate Console"
Marca "Launch in background"
Marca "Run the builder After a Clean"
Marca "Run the builder During manual builds"
Marca "Run the builder During auto builds"
Marca "Specify working set of relevant resources"
Clickea en el boton "Specify Resources"
Selecciona el directorio JNI de tu proyecto y todos los ficheros de dentro.
Ahora clicke en el boton OK.


Para la explicacion hemos asumido que Cygwin esta instalado en c:\cygwin, que la NDK esta en c:\Android_NDK y que el proyecto se llama myapp, tendreis que cambiarlo convenientemente.


Bueno, teniendo ya esto, Eclipse hara todo por ti, espero que os sirva, y que os pongais todos a programar rapidamente con la NDK.

lunes, 15 de marzo de 2010

Cambiando transparencia y color de un Bitmap


Nuestro objetivo aquí es con una sola imagen crear diferentes versiones cambiando la transparencia y el color sin tener que usar 100 imagenes.

Transparencia

Para añadir transparencia a una imagen o Bitmap, debemos hacerlo mediante el uso del objeto Paint. Quedando de la siguiente forma:
Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.placew);
Paint mask = new Paint();
mask.setAlpha(255/10 * j);
canvas.drawBitmap(b,0,0, mask);


Con el método setAlpha podemos cambiar la transparencia desde opaco 255 a invisible 0.

Color

La tarea de cambiar el color es un tanto mas compleja.Para esto usaremos la imagen que queramos modificar con el area al que le queramos cambiar el color, de color blanco. Y cuando queramos cambiarle el color lo que haremos será recorrer la imagen pixel a pixel, y cuando el pixel sea blanco lo cambiaremos por el que queramos.


A continuación dejamos un ejemplo del código de la aplicación mostrada en la imagen donde se aplica todo lo comentado.


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}

private static class SampleView extends View {

public SampleView(Context context) {
super(context);
this.setBackgroundColor(Color.WHITE);
}

@Override
protected void onDraw(Canvas canvas) {

for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
Bitmap b =
BitmapFactory.decodeResource(getResources(),R.drawable.placew);
Paint mask = new Paint();
mask.setAlpha(255/10 * j);
int color = Color.rgb(255 - (255/10 * i),(255/10 * i) , 0);
canvas.drawBitmap(adjust(b,color ), 0 + (b.getWidth() * j), i * b.getHeight() + 15,
mask);
}}}}

private static Bitmap adjust(Bitmap d, int color){

Bitmap src = d;
Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
for(int x = 0;x < bitmap.getWidth();x++)
for(int y = 0;y < bitmap.getHeight();y++)
if(match(bitmap.getPixel(x, y)))
bitmap.setPixel(x, y, color);
return bitmap;
}

private static boolean match(int pixel){

int[] FROM_COLOR = {255,255,255};
int THRESHOLD = 10;

return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}

jueves, 11 de marzo de 2010

Issue in 2.1: onStop()/onDestroy() delayed...

Dianne Hackbor has recently posted at groups.google.com/group/android-developers this about the activities lifecycle in Android 2.1.

Hi, there is an issue in 2.1 with the new launcher that causes the onStop()/onDestroy()to be delayed longer than typical when going back to home. Note these are not delayed -forever-, but just until later in the future (when the next activity switch happens).

These methods are defined to happen at "some time in the future" so technically it is not really broken, but this is certainly not the desired behavior here. This will befixed in the next release.

Fwiw, this happened because the new launcher has a special surface view on top of itself to display the 3d all apps, and this caused the window manager to be confused and not realize that the app behind it had been hidden so it could then be stopped/destroyed.


It looks like these basic methods in android activities have not the desired behavior.
For example in my case when the current activity called the onStop() method it should kill a background service, but because this issue as this method has a delay, when the OS really calls onStop() it caused a disaster in my application.

Solution: instead of using onStart() and onStop() in order to manage the background service I use the onResume() and onPause() methods.