I created an application in android studio but when I install on the phone and game is slow

Asked

Viewed 68 times

0

  • Good afternoon, I created a small application and went to the phone to try how well it ran on my phone I gave it to my friends to see if it worked on their phone. I ended up discovering that in some it gets slow and in others fast. Getting really slow on good capacity phones. I need help.

Code (Main_activity):

public class MainActivity extends AppCompatActivity {


Button b_flip1;

Button b_flip2;

ImageView iv_coin1;

ImageView iv_coin2;
Random r;

Random r1;

int coinSide;

int coinSide1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    b_flip1 = (Button) findViewById(R.id.b_flip1);
    b_flip2 = (Button)  findViewById(R.id.b_flip2);
    iv_coin1 = (ImageView) findViewById(R.id.iv_coin1);
    iv_coin2 = (ImageView) findViewById(R.id.iv_coin2);
    r = new Random();
    r1 = new Random();

    //codigo button 1 ............................................................................


    b_flip1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            coinSide = r.nextInt(7);

            if (coinSide == 0){
                iv_coin1.setImageResource(R.drawable.ummpt);

            } else if (coinSide == 1){
                iv_coin1.setImageResource(R.drawable.doissspt);

            } else if (coinSide == 2){
                iv_coin1.setImageResource(R.drawable.tressspt);


            }else if (coinSide == 3){
                iv_coin1.setImageResource(R.drawable.quatrooopt);

            }else  if (coinSide == 4){
                iv_coin1.setImageResource(R.drawable.cincoopt);

            }else if (coinSide == 5){
                iv_coin1.setImageResource(R.drawable.seissspt);

            }


            RotateAnimation rotate = new RotateAnimation(0,360,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            rotate.setDuration(1000);
            iv_coin1.startAnimation(rotate);

        }
    });



    //codigo button 2 .............................................................................


    b_flip2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            coinSide1 = r1.nextInt(7);

            if (coinSide1 == 0){
                iv_coin2.setImageResource(R.drawable.seteept);

            } else if (coinSide1 == 1){
                iv_coin2.setImageResource(R.drawable.oitoopt);

            } else if (coinSide1 == 2){
                iv_coin2.setImageResource(R.drawable.noveept);


            }else if (coinSide1 == 3){
                iv_coin2.setImageResource(R.drawable.dezzpt);

            }else  if (coinSide1 == 4){
                iv_coin2.setImageResource(R.drawable.onzeept);

            }else if (coinSide1 == 5){
                iv_coin2.setImageResource(R.drawable.dozeeept);

            }

            RotateAnimation rotate = new RotateAnimation(0,360,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            rotate.setDuration(1000);
            iv_coin2.startAnimation(rotate);

        }
    });

}

}

.................................................................

Layout

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ummpt"
            android:elevation="21dp"
            android:id="@+id/iv_coin1"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="21dp"
            android:id="@+id/iv_coin2"
            android:layout_weight="1"
            app:srcCompat="@drawable/seteept" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:id="@+id/b_flip1"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/buttondois" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/b_flip2"
            android:layout_weight="1"
            android:background="@drawable/buttondois" />
    </LinearLayout>
</LinearLayout>

  • 1

    You can clarify what slows down?

  • Application has two wheels that rotate when you click a button.Each wheel has a button. They rotate change the image of the wheel.. And this part is slow.. ON some phones, when you click the button the wheels rotate very slowly , even taking 20 seconds..

  • You can put them spinning inside a trhead, one for each, then vacate the main thread and leave them in the daughters..

1 answer

0


Try using threads for each "wheel" you have, so you won’t run them in the main thread.

new Thread(new Runnable() {
            @Override
            public void run() {
                RotateAnimation rotate = new RotateAnimation(0,360,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            rotate.setDuration(1000);
            iv_coin1.startAnimation(rotate);
    }
}).start();
  • Thank you very much, I’ll try as soon as I have time ;).

  • So it turns the first time and the others no longer turn..

  • You have to implement the thread call when you click on the wheel...

  • I’m new at this I don’t know how to do that part...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.