启动画面 Android - 倒数计时器显示

Splash Screen Android - Count Down Timer Display

我正在尝试为我的应用程序使用启动画面。 我想在启动画面中显示倒数计时器。 到目前为止,我能够实现启动画面的工作,但不知道如何显示倒数计时器。我为倒数计时器创建了布局。我希望启动画面停留 5 小时,然后转到下一个 activity。

Splashactivity.java

   public class splash_screen extends AppCompatActivity {
    private float imageAplha = 1f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        //splashScreenUseTimer(5000);
        splashScreenUseAsyncTask();
    }


    // Show splash screen until network load data complete.
    private void splashScreenUseAsyncTask()
    {
        // Create a AsyncTask object.
        final RetrieveDateTask retrieveDateTask = new RetrieveDateTask();
        retrieveDateTask.execute("", "", "");
        // Get splash image view object.
        final ImageView splashImageView = (ImageView) findViewById(R.id.logo_id);

        // Create a count down timer object.It will count down every 0.1 seconds and last for milliSeconds milliseconds..
        CountDownTimer countDownTimer = new CountDownTimer(5000, 100) {
            @Override
            public void onTick(long l) {
                // Reduce the splash screen background image's alpha value for each count down.
                splashImageView.setAlpha(imageAplha);
                imageAplha -= 0.1;
                if(imageAplha <= 0)
                {
                    imageAplha = 1;
                }
            }

            @Override
            public void onFinish() {
                // When count down complete, set the image to invisible.
                imageAplha = 0;
                splashImageView.setAlpha(imageAplha);

                // If AsyncTask is not complete, restart the counter to count again.
                if(!retrieveDateTask.isAsyncTaskComplete()) {
                    this.start();
                }
            }
        };
        // Start the count down timer.
        countDownTimer.start();
    }

    // This is the async task class that get data from network.
    private class RetrieveDateTask extends AsyncTask<String, String, String>{

        // Indicate whether AsyncTask complete or not.
        private boolean asyncTaskComplete = false;

        public boolean isAsyncTaskComplete() {
            return asyncTaskComplete;
        }

        public void setAsyncTaskComplete(boolean asyncTaskComplete) {
            this.asyncTaskComplete = asyncTaskComplete;
        }

        // This method will be called before AsyncTask run.
        @Override
        protected void onPreExecute() {
            this.asyncTaskComplete = false;
        }

        // This method will be called when AsyncTask run.
        @Override
        protected String doInBackground(String... strings) {
            try {
                // Simulate a network operation which will last for 10 seconds.
                Thread currTread = Thread.currentThread();
                for (int i = 0; i < 10; i++) {
                    currTread.sleep(1000);
                }
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }finally {
                return null;
            }
        }

        // This method will be called after AsyncTask run.
        @Override
        protected void onPostExecute(String s) {
            // Start SplashScreenMainActivity.
            Intent mainIntent = new Intent(splash_screen.this,
                    MainActivity.class);
            splash_screen.this.startActivity(mainIntent);
            // Close SplashScreenActivity.
            splash_screen.this.finish();
            this.asyncTaskComplete = true;
        }
    }
}

布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0"
    android:background="@color/White"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/logo_id"
            android:layout_width="350dp"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/ts_logo"
            android:layout_gravity="center"/>
    </LinearLayout>

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

        <LinearLayout
            android:id="@+id/linear_layout_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/black"
            android:visibility="visible">

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

                <TextView
                    android:id="@+id/tv_hour"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="00"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/tv_hour_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Hour"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="normal" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/tv_minute"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="00"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/tv_minute_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Minute"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="normal" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/tv_second"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="00"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/tv_second_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Second"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="normal" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

@contact dummy 我已经编辑了你的代码尝试使用这个

// Show splash screen until network load data complete.
private void splashScreenUseAsyncTask()
{
    // Create a AsyncTask object.
    final RetrieveDateTask retrieveDateTask = new RetrieveDateTask();
    retrieveDateTask.execute("", "", "");
    // Get splash image view object.
    final ImageView splashImageView = (ImageView) findViewById(R.id.logo_id);

     //for 5 Hours 
    final int time= 3600000*5;          
    CountDownTimer countDownTimer = new CountDownTimer(time, 1000) {
        @Override
        public void onTick(long l) {


                    long Days = l / (24 * 60 * 60 * 1000);
                    long Hours = l / (60 * 60 * 1000) % 24;
                    long Minutes = l / (60 * 1000) % 60;
                    long Seconds = l / 1000 % 60;
                    //
                    tv_days.setText(String.format("%02d", Days));
                    tv_hour.setText(String.format("%02d", Hours));
                    tv_minute.setText(String.format("%02d", Minutes));
                    tv_second.setText(String.format("%02d", Seconds));

            splashImageView.setAlpha(imageAplha);
            imageAplha -= 0.1;
            if(imageAplha <= 0)
            {
                imageAplha = 1;
            }
        }

        @Override
        public void onFinish() {
            // When count down complete, set the image to invisible.
            imageAplha = 0;
            splashImageView.setAlpha(imageAplha);

            // If AsyncTask is not complete, restart the counter to count again.
            if(!retrieveDateTask.isAsyncTaskComplete()) {
                this.start();
            }
        }
    };
    // Start the count down timer.
    countDownTimer.start();
}

@contact dummy 我用小 modification.try 编辑了@Dev 代码。根据您的动画要求增加和减少 imageAlpha 大小。

private float imageAplha = 1f;
private boolean imageStatus = false;

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


// Show splash screen until network load data complete.
private void splashScreenUseAsyncTask()
{
    // Create a AsyncTask object.
    final RetrieveDateTask retrieveDateTask = new RetrieveDateTask();
    retrieveDateTask.execute("", "", "");
    // Get splash image view object.
    final ImageView splashImageView = (ImageView) findViewById(R.id.logo_id);
    final TextView tv_hour = (TextView) findViewById(R.id.tv_hour);
    final TextView tv_minute = (TextView) findViewById(R.id.tv_minute);
    final TextView tv_second = (TextView) findViewById(R.id.tv_second);

    // Create a count down timer object.It will count down every 0.1 seconds and last for milliSeconds milliseconds..
    final int time= 3600000*5;
    CountDownTimer countDownTimer = new CountDownTimer(time, 1000) {
        @Override
        public void onTick(long l) {


            long Days = l / (24 * 60 * 60 * 1000);
            long Hours = l / (60 * 60 * 1000) % 24;
            long Minutes = l / (60 * 1000) % 60;
            long Seconds = l / 1000 % 60;
            //
          //  tv_days.setText(String.format("%02d", Days));
            tv_hour.setText(String.format("%02d", Hours));
            tv_minute.setText(String.format("%02d", Minutes));
            tv_second.setText(String.format("%02d", Seconds));

            splashImageView.setAlpha(imageAplha);


            if(imageStatus){
                imageAplha += 1;
                if(imageAplha >= 1)
                {
                   // imageAplha-= 0.5;
                    imageStatus =  false;
                }
            }else{
                imageAplha -= 1;
                if(imageAplha <= 0)
                {

                    imageStatus =  true;
                }
            }

        }

        @Override
        public void onFinish() {
            // When count down complete, set the image to invisible.
            imageAplha = 0;
            splashImageView.setAlpha(imageAplha);

            // If AsyncTask is not complete, restart the counter to count again.
            if(!retrieveDateTask.isAsyncTaskComplete()) {
                this.start();
            }
        }
    };
    // Start the count down timer.
    countDownTimer.start();
}

// This is the async task class that get data from network.
private class RetrieveDateTask extends AsyncTask<String, String, String> {

    // Indicate whether AsyncTask complete or not.
    private boolean asyncTaskComplete = false;

    public boolean isAsyncTaskComplete() {
        return asyncTaskComplete;
    }

    public void setAsyncTaskComplete(boolean asyncTaskComplete) {
        this.asyncTaskComplete = asyncTaskComplete;
    }

    // This method will be called before AsyncTask run.
    @Override
    protected void onPreExecute() {
        this.asyncTaskComplete = false;
    }

    // This method will be called when AsyncTask run.
    @Override
    protected String doInBackground(String... strings) {
        try {
            // Simulate a network operation which will last for 10 seconds.
            Thread currTread = Thread.currentThread();
            for (int i = 0; i < 18000000; i++) {
                currTread.sleep(1000);
            }
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }finally {
            return null;
        }
    }

    // This method will be called after AsyncTask run.
    @Override
    protected void onPostExecute(String s) {
        // Start SplashScreenMainActivity.
        Intent mainIntent = new Intent(splash_screen .this,
                MainActvity.class);
        splash_screen.this.startActivity(mainIntent);
        // Close SplashScreenActivity.
        splash_screen.this.finish();
        this.asyncTaskComplete = true;
    }
}

}