Кнопка активности Android onClick () работает медленно

В моем событии кнопки Activity работает медленно, в этом действии все кнопки работают нормально, но они реагируют с небольшой задержкой около 2/3 секунды. Я делаю здесь, когда я нажимаю кнопку, он меняет весь фон кнопки. Я не могу понять, в чем проблема, пожалуйста, помогите мне!

Activity file

public class TaskBarView extends Activity implements OnClickListener {

private int time = 0;
private Button btn_fiveSec,btn_tenSec,btn_fifteenSec,btn_thirtySec,btn_oneMin,btn_fiveMin,btn_tenMin,btn_thirtyMin;
private TextView tv_viewTime;
Handler myHandler;
boolean startPrank;

public void onBackPressed() {
    finish();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myHandler = new Handler();

    final Runnable mMyRunnable = new Runnable() {
        @Override
        public void run(){

            if (Build.VERSION.SDK_INT >= 23) {
                if (!Settings.canDrawOverlays(TaskBarView.this)) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                    startActivityForResult(intent, 1234);
                } else {
                    playSound();
                    Intent intent = new Intent(TaskBarView.this, CrackScreen.class);
                    startService(intent);
                    finish();
                }
            } else{
                playSound();
                Intent intent = new Intent(TaskBarView.this, CrackScreen.class);
                startService(intent);
                finish();
            }
        }
    };

    Button btn_startPrank = findViewById(R.id.btn_startPrank);
    btn_startPrank.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            startPrank = true;

            showToast(startPrank);

            int de = time * 1000;
            myHandler.postDelayed(mMyRunnable, de);
        }
    });

    btn_fiveSec = findViewById(R.id.btn_fiveSec);
    btn_fiveSec.setOnClickListener(this);

    btn_tenSec = findViewById(R.id.btn_tenSec);
    btn_tenSec.setOnClickListener(this);

    btn_fifteenSec = findViewById(R.id.btn_fifteenSec);
    btn_fifteenSec.setOnClickListener(this);

    btn_thirtySec = findViewById(R.id.btn_thirtySec);
    btn_thirtySec.setOnClickListener(this);

    btn_oneMin = findViewById(R.id.btn_oneMin);
    btn_oneMin.setOnClickListener(this);

    btn_fiveMin = findViewById(R.id.btn_fiveMin);
    btn_fiveMin.setOnClickListener(this);

    btn_tenMin = findViewById(R.id.btn_tenMin);
    btn_tenMin.setOnClickListener(this);

    btn_thirtyMin = findViewById(R.id.btn_thirtyMin);
    btn_thirtyMin.setOnClickListener(this);


    Button btn_stopPrank = findViewById(R.id.btn_stopPrank);
    btn_stopPrank.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            myHandler.removeCallbacks(mMyRunnable);
            startPrank = false;
            Intent intent = new Intent(TaskBarView.this, CrackScreen.class);
            stopService(intent);
            tv_viewTime.setText("Cracked Screen stopped");

            showToast(startPrank);
        }
    });

    tv_viewTime = findViewById(R.id.tv_viewTime);
    tv_viewTime.setText("Cracked Screen starts with in 0 second");

}

private void playSound(){
    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.glass_break);
    mp.start();
}

private void showToast(boolean prank){
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

    TextView tv_toastMessage = layout.findViewById(R.id.tv_toastMessage);
    RelativeLayout relativeLayout = layout.findViewById(R.id.re_toastBackground);

    if (startPrank){
        relativeLayout.setBackgroundResource(R.drawable.notification_start);
        tv_toastMessage.setText("Start Pranking");
        tv_toastMessage.setTextColor(Color.parseColor("#0AB1E9"));
    } else if (!startPrank){
        relativeLayout.setBackgroundResource(R.drawable.notification_stop);
        tv_toastMessage.setText("Stop Pranking");
        tv_toastMessage.setTextColor(Color.parseColor("#F83668"));
    }else {
        relativeLayout.setBackgroundColor(Color.parseColor("#6651D4"));
        tv_toastMessage.setText("Just Fun");
    }

    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER,0,0);
    toast.setView(layout);
    toast.show();
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_fiveSec:
            time = 5;

            btn_fiveSec.setBackgroundResource(R.drawable.time_select);
            btn_fiveSec.setTextColor(Color.WHITE);
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 5 second");

            break;

        case R.id.btn_tenSec:
            time = 10;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time_select);
            btn_tenSec.setTextColor(Color.WHITE);
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 10 second");

            break;

        case R.id.btn_fifteenSec:
            time = 15;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time_select);
            btn_fifteenSec.setTextColor(Color.WHITE);
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 15 second");

            break;

        case R.id.btn_thirtySec:
            time = 30;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time_select);
            btn_thirtySec.setTextColor(Color.WHITE);
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 30 second");

            break;

        case R.id.btn_oneMin:
            time = 60;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time_select);
            btn_oneMin.setTextColor(Color.WHITE);
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 1 Minute");

            break;

        case R.id.btn_fiveMin:
            time = 300;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time_select);
            btn_fiveMin.setTextColor(Color.WHITE);
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 5 Minute");

            break;

        case R.id.btn_tenMin:
            time = 600;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time_select);
            btn_tenMin.setTextColor(Color.WHITE);
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 10 Minute");

            break;

        case R.id.btn_thirtyMin:
            time = 1800;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time_select);
            btn_thirtyMin.setTextColor(Color.WHITE);
            tv_viewTime.setText("Cracked Screen starts with in 30 Minute");

            break;

    }
}

}

xml file

<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background = "@drawable/bg">

<RelativeLayout
    android:id = "@+id/prank"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_centerInParent = "true">

<RelativeLayout
    android:id = "@+id/rel_1"
    android:layout_width = "match_parent"
    android:layout_height = "278dp"
    android:background = "@drawable/bg_white">

    <TextView
        android:id = "@+id/tv_setTime"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_alignParentTop = "true"
        android:layout_marginTop = "22dp"
        android:gravity = "center"
        android:text = "@string/set_time"
        android:textColor = "#6651d4"
        android:textSize = "25sp" />

    <RelativeLayout
        android:id = "@+id/rl_btnSec"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_below = "@+id/tv_setTime"
        android:gravity = "center"
        android:layout_centerInParent = "true">

        <Button
            android:id = "@+id/btn_fiveSec"
            android:layout_width = "65sp"
            android:layout_height = "65sp"
            android:layout_marginTop = "10sp"
            android:background = "@drawable/time"
            android:text = "@string/fiveSecond"
            android:textAllCaps = "false"
            android:textSize = "20sp"
            android:textColor = "#6651d4"
            android:paddingBottom = "15sp"/>

        <Button
            android:id = "@+id/btn_tenSec"
            android:layout_width = "65sp"
            android:layout_height = "65sp"
            android:layout_marginTop = "10sp"
            android:layout_marginLeft = "5sp"
            android:background = "@drawable/time"
            android:layout_toEndOf = "@+id/btn_fiveSec"
            android:layout_toRightOf = "@+id/btn_fiveSec"
            android:text = "@string/tenSecond"
            android:textAllCaps = "false"
            android:textSize = "20sp"
            android:textColor = "#6651d4"
            android:paddingBottom = "15sp"
            android:layout_marginStart = "5sp" />

        <Button
            android:id = "@+id/btn_fifteenSec"
            android:layout_width = "65sp"
            android:layout_height = "65sp"
            android:layout_marginTop = "10sp"
            android:background = "@drawable/time"
            android:layout_marginLeft = "5sp"
            android:layout_toEndOf = "@+id/btn_tenSec"
            android:layout_toRightOf = "@+id/btn_tenSec"
            android:text = "@string/fifteenSecond"
            android:textAllCaps = "false"
            android:textSize = "20sp"
            android:textColor = "#6651d4"
            android:paddingBottom = "15sp"
            android:layout_marginStart = "5sp" />

        <Button
            android:id = "@+id/btn_thirtySec"
            android:layout_width = "65sp"
            android:layout_height = "65sp"
            android:layout_marginTop = "10sp"
            android:layout_marginLeft = "5sp"
            android:background = "@drawable/time"
            android:layout_toEndOf = "@+id/btn_fifteenSec"
            android:layout_toRightOf = "@+id/btn_fifteenSec"
            android:text = "@string/thirtySecond"
            android:textAllCaps = "false"
            android:textSize = "20sp"
            android:textColor = "#6651d4"
            android:paddingBottom = "15sp"
            android:layout_marginStart = "5sp" />

    </RelativeLayout>

    <RelativeLayout
        android:id = "@+id/rl_btnMin"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_below = "@+id/rl_btnSec"
        android:gravity = "center"
        android:layout_centerInParent = "true">

        <Button
            android:id = "@+id/btn_oneMin"
            android:layout_width = "65sp"
            android:layout_height = "65sp"
            android:layout_marginTop = "10sp"
            android:background = "@drawable/time"
            android:text = "@string/oneMinute"
            android:textAllCaps = "false"
            android:textSize = "20sp"
            android:textColor = "#6651d4"
            android:paddingBottom = "15sp"/>

        <Button
            android:id = "@+id/btn_fiveMin"
            android:layout_width = "65sp"
            android:layout_height = "65sp"
            android:layout_marginTop = "10sp"
            android:layout_marginLeft = "5sp"
            android:background = "@drawable/time"
            android:layout_toEndOf = "@+id/btn_oneMin"
            android:layout_toRightOf = "@+id/btn_oneMin"
            android:layout_marginStart = "5sp"
            android:text = "@string/fiveMinute"
            android:textAllCaps = "false"
            android:textSize = "20sp"
            android:textColor = "#6651d4"
            android:paddingBottom = "15sp"/>

        <Button
            android:id = "@+id/btn_tenMin"
            android:layout_width = "65sp"
            android:layout_height = "65sp"
            android:layout_marginTop = "10sp"
            android:layout_marginLeft = "5sp"
            android:background = "@drawable/time"
            android:layout_toEndOf = "@+id/btn_fiveMin"
            android:layout_toRightOf = "@+id/btn_fiveMin"
            android:layout_marginStart = "5sp"
            android:text = "@string/tenMinute"
            android:textAllCaps = "false"
            android:textSize = "20sp"
            android:textColor = "#6651d4"
            android:paddingBottom = "15sp"/>

        <Button
            android:id = "@+id/btn_thirtyMin"
            android:layout_width = "65sp"
            android:layout_height = "65sp"
            android:layout_marginTop = "10sp"
            android:layout_marginLeft = "5sp"
            android:background = "@drawable/time"
            android:layout_toEndOf = "@+id/btn_tenMin"
            android:layout_toRightOf = "@+id/btn_tenMin"
            android:layout_marginStart = "5sp"
            android:text = "@string/thirtyMinute"
            android:textAllCaps = "false"
            android:textSize = "20sp"
            android:textColor = "#6651d4"
            android:paddingBottom = "15sp"/>

    </RelativeLayout>

    <TextView
        android:id = "@+id/tv_viewTime"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_below = "@+id/rl_btnMin"
        android:layout_marginTop = "10sp"
        android:gravity = "center"
        android:textColor = "#6651d4"
        android:textSize = "15sp" />

</RelativeLayout>

<Button
    android:id = "@+id/btn_startPrank"
    android:layout_width = "200sp"
    android:layout_height = "60sp"
    android:layout_below = "@+id/rel_1"
    android:text = "@string/start_pranking"
    android:textColor = "@android:color/white"
    android:layout_centerHorizontal = "true"
    android:background = "@drawable/start"/>

<Button
    android:id = "@+id/btn_stopPrank"
    android:layout_width = "200sp"
    android:layout_height = "60sp"
    android:layout_below = "@+id/btn_startPrank"
    android:text = "@string/stop_pranking"
    android:textColor = "@android:color/white"
    android:layout_centerHorizontal = "true"
    android:background = "@drawable/off"/>

</RelativeLayout>

Я использую здесь Thread. Любые предложения рассматривайте как большую помощь. Спасибо!

почему вы используете postDelay? Кажется, вы откладываете свое действие, а не onClick, он медленный.

aolphn 04.11.2018 13:23

Здесь требуется некоторое профилирование производительности, но похоже, что задержка вызвана группами операторов setBackgroundResource(). Почему бы вам не попробовать это с прокомментированными вызовами, за исключением, возможно, btn_fiveSec, и если это сработает лучше, я предложу объяснение.

greeble31 04.11.2018 15:17
1
2
261
0

Другие вопросы по теме