Tuesday, November 8, 2016

java - Game is responding very slowly?



I have a very simple app which removes buttons and re-adds buttons to certain layouts when a button is clicked. The problem is, this process is taking a very long time. My question is: How can I make this process faster


I have a simple program which swaps two buttons:



  1. Remove view (button)

  2. Generate random number, and from my button array list, get the button in the array list with the index of that random number

  3. Remove that random view (button)

  4. Add the two buttons in each others places.


Now, the problem is that I want the buttons to swap places right after I click on a button. Instead, the program is taking about three seconds to do the above four tasks after I click the button.


Here is the code:



    red.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {

// Remove the clicked button from it's frame layout parent.
ViewManager p = (ViewManager) v.getParent();
p.removeView(v);
// Find another button randomly.
Random rng = new Random();
View v2;

do
{
int i = rng.nextInt(4);
v2 = buttons.get(i);
}
while (v2 == v); // Loop until you get a different button to swap with.
// Remove the other button from it's frame layout parent.
ViewManager p2 = (ViewManager) v2.getParent();
p2.removeView(v2);
// Now simply insert each button into the other buttons frame layout.

p.addView(v2, v2.getLayoutParams()); //Adding v2, the random button, into where v, the button clicked, was before(p)!
p2.addView(v, v.getLayoutParams()); //Adding v, the button clicked, into where v2, the random button, was before(p2)
}
}
});

So, having done my research, I learned that one thing that may be slowing me down could be the fact that the main UI is handling all of this work. So, I decided to put all of this code inside of a thread to run in the background:


 red.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {


new Thread(new Runnable() {

@Override
public void run() {

//ALL THE CODE I PROVIDED UP THERE

}).start();


But putting this code in a thread gave me another exception. This is probably happening because I am trying to update the UI (remove and add buttons) from the thread.



 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

So, I don't know how I should approach this problem, since I do need to change my views...


Another thing that could be slowing down my app (from the research I did) is that my layout might be inflating after a while, due to such a giant layout. My layout is pretty big, so that may be it:


enter image description here


I doubt it is the layout though, as the screen loads pretty fast. What happens is that when I click a button, the screen freezes for three seconds, and then updates the UI with the completed tasks.


Could I somehow take up more memory to be faster as an app? The whole basis of my app runs on being able to complete those four tasks and update the UI as soon as the user presses on a button. What else should I do? I have also added this line in my manifest after looking at similar questions:


android:largeHeap = "true"

Now, what else should I do? Thanks so much for your help, I really appreciate it. I have been stuck on this all day...so I really (REALLY!) would like any feedback.



Thanks so much for your time




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...