当前位置: 代码迷 >> java >> 在游戏屏幕中央具有透明文本视图的倒数计时器
  详细解决方案

在游戏屏幕中央具有透明文本视图的倒数计时器

热度:93   发布时间:2023-07-26 13:50:29.0

我正在开发游戏。 在我的游戏中,我有一个对话框来关闭并继续游戏。我想要的事情是当我按下对话框的“否”按钮,然后从3开始计数到0,然后继续游戏。 请告诉我如何在游戏屏幕中央以透明的textview实现倒数计时器,并且3秒钟后计时器也不可见。 请通过编辑代码解决我的问题。 这是我的AlertDialog代码

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {


    GamePanel.thread.setRunning(false);
    // in the next line of code we also style the dialog through xml which i put in styles
    AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
    alertDialog.setTitle("Exit Alert");
    alertDialog.setMessage("Do you really want to exit the Game?");
    alertDialog.setButton("Yes", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which) {
            //Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
            // A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
            finish();
            System.exit(0);
            return;

        } });
    alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();




            GamePanel.thread.setRunning(true);
            return;
        }});
    alertDialog.show();

    return true;
}
return super.onKeyDown(keyCode, event);}

任何帮助,将不胜感激。 谢谢!

    final TextView tv = new TextView(this);
    final PopupWindow popupWindow = new PopupWindow(tv, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);

    popupWindow.showAtLocation(getCurrentFocus(), Gravity.CENTER,0,0);
    new AsyncTask<Void,Integer,Void>(){
        @Override
        protected Void doInBackground(Void... voids) {
            int inc = 3 ;
            while(true){
                publishProgress(inc);
                inc -- ;
                if(inc == 0 ){
                    break;
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            popupWindow.dismiss();
            GamePanel.thread.setRunning(true);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            tv.setText(values[0]+"");

        }
    }.execute();

您的button2 onclicklistener中的这段代码怎么样? 这个想法,可能是有一个bug。 希望可以帮到您。

  相关解决方案