I'm starting to program for android, and I've made a memory game, that of matching cards.
The game is going well, there are no code problems, but in the tests that I have done with the devices that I have at home, I see that in a cell phone it is much slower than in the other devices. I mean, I tried it on an old galaxy and a tablet with jelly bean, a tablet with kitkat, a tablet with nougat, a samsung with nougat and a sony with marshmallow.
The funny thing is that in the newer mobiles (the sony m4 and the samsung A5) the game is overly slow, and in the others it goes well.
As for the code, it's simple. There are several layout with different number of letters to make pairs, from 8 to 32. Each letter consists of an ImageView in which I indicate a background that acts as a frame, and an image that makes the front of the letter.
<ImageView
android:id="@+id/carta3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="@dimen/margenCartasJuego"
android:padding="@dimen/paddinCartasJuego"
android:background="@color/colorFondoCartasJuego"
android:scaleType="fitXY"
android:src="@drawable/logo"
/>
The logic is simple too.
In summary form.
When starting the activity, randomly assign the routes of the different images, for example:
1º- in a string variable called rutaCarta1 is assigned the rutaFoto4.
2º- to the ImageView with id carta1 is set with setImageUri (Uri.parse (rutaCarta1)), it is also scaled with scaleType and the alpha of the image is set to 0 so that the photo can not be seen and the src is visible , that does of obverse.
3º- when clicking on the letter, the alpha of the image is set to 255 so that it is seen, and it is compared with the other letter if there is one to see if they are even.
carta1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
*//si es la segunda carta inhabilita el resto de cartas hasta
//que se comprueba si hacen pareja o no.*
if(jugando){
inhabilitarCartas();
}
//crea un hilo a parte para cambiar el alpha del ImageView.
new Thread(new Runnable() {
public void run() {
carta1.post(new Runnable() {
@Override
public void run() {
carta1.setImageAlpha(255);
}
});
}
}).start();
*//añadí un retardo antes de llamar a la funcion que
//comprueba si hay pareja, para asegurarme que en el
//momento de ejecutar el hilo de cambiar el alpha no
//habia mas código ejecutandose que le pudiera afectar.
//
//tanto el poner el código en un hilo a parte como el
//retardo ayudaron a que mostrase la imagen mas rápido.*
retardoFuncionComprobarCarta(0);
}
});
The more cards there are in the slower layout, the images have a size between 6 and 40kb.
In the mobile that is slow, it is noted that since you press until you see the image, spend 2 to 5 seconds, depending on the number of cards that layout has, however, in other devices it is almost immediate, and the difference between the layout with 8 letters and that of 32 is minimal.
I have changed the way of "uncovering" the image several times with the same result (or worse), at the beginning it did not change the alpha to 0, and pressing the letter is when the image was set with the setImageUri.
I hope to have explained myself well. Thanks.
Hello, reviewing the code I found the part that slows down the game on new phones.
When you uncover the second card, call a function (disable Cards) that disables all cards so you can not continue playing while checking for a match. This is the code of the function:
public void inhabilitarCartas(){
//inhabilita las cartas para que no se pueda pulsar mas cartas mientras se muestran la cartas
// seleccionadas, para que el jugador pueda memorizar la posicion de estas cartas.
for (int i = 0; i <= 15; i++) {
imageViewReseteoGeneral = (ImageView) findViewById(idCartas[i]);
imageViewReseteoGeneral.setEnabled(false);
}
}
I modified the game, adding a conditioner to the OnClickListener events to solve it.
carta1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//comprueba si ya hay dos cartas giradas.
//si no las hay, gira la carta y hace la comprobacion de pareja.Si hay dos cartas giradas, no hace nada.
if(!comprobando){
//si jugando es true, es que és la segunda carta seleccionada, y pone
// comprobando en true para que no se ejecute otros onclicklistener.
if(jugando){
comprobando=true;
}
//muestra la carta en un hilo a parte.
new Thread(new Runnable() {
public void run() {
carta1.post(new Runnable() {
@Override
public void run() {
carta1.setImageAlpha(255);
}
});
}
}).start();
//comprueba la carta.
retardoFuncionComprobarCarta(0);
}
//si ya hay dos cartas giradas, no hace nada.
else {
return;
}
}
});
Now it's faster, almost the same as in the other devices.
But I still have the same doubt, and I do not know why, this feature slows down the game on the devices that have better hardware and a newer version of android, and on the old ones or on the tablets it works well.
Thanks again.
And happy year.