递增计时器 - 不知道如何暂停和继续



我有一个Android活动,我希望计时器在打开时启动,但是当您单击屏幕时,我希望暂停符号显示和计时器停止,然后当您单击时屏幕再次将计时器接收到它离开并继续的位置。有点像一个切换功能。

我的暂停按钮可以很好地切换,并且计时器开始计数良好,但是我不知道如何暂停并在每次点击之间重新播放。我该怎么做?

我有一个名为" Mivisible"的私人布尔值,因为我只在"暂停按钮"是看不见的或(可见的(时才尝试播放计时器,但是我只是得到一个无效的指针例外。

谢谢!

Java代码:

package com.example.jonathan.om11;
import android.media.Image;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;
public class Environment1Activity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "Environment1Activity";
    //Initiate for Timer
    protected int count = 0;
    private ImageView playerPause;
    private ImageView backgroundImageBtn;
    private boolean mVisible;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_environment1);
        hidePauseBtn();
        timer();
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        playerPause = (ImageView) findViewById(R.id.pauseBtn);
        playerPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });
        backgroundImageBtn = (ImageView) findViewById(R.id.rainforest_img);
        backgroundImageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });
    }
    private void toggle() {
        if (mVisible) {
            hide();
        } else {
            show();
        }
    }
    //Pause
    public void show() {
        playerPause = (ImageView) findViewById(R.id.pauseBtn);
        playerPause.setVisibility(View.VISIBLE);
        mVisible = true;
        Log.d(TAG, "Player: PAUSED ");
    }

    //Play
    public void hide() {
        playerPause = (ImageView) findViewById(R.id.pauseBtn);
        playerPause.setVisibility(View.GONE);
        mVisible = false;
    }
    //Hide pause button upon creating activity
    public void hidePauseBtn() {
        playerPause = (ImageView) findViewById(R.id.pauseBtn);
        playerPause.setVisibility(View.GONE);
    }
    @Override
    public void onClick(View v) {
    }
    public void timer() {
        Timer timer = new Timer();
        //Count environment runtime
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        count++;
                        Log.d(TAG, "run: Time is " + count);
                    }
                });
            }
        }, 1000, 1000);
    }
}

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jonathan.om11.Environment1Activity">
        <ImageView
            android:id="@+id/rainforest_img"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/rainforest_env" />
        <ImageView
            android:id="@+id/pauseBtn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toStartOf="@+id/rainforest_img"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@drawable/ic_pause_white_48dp" />
    </android.support.constraint.ConstraintLayout>

在您的暂停按钮上单击时,当您切换ANS显示播放按钮时,也停止计时器或将其删除以停止计时器,然后在单击播放按钮时,您可以重新运行一个计时器的新实例,但它将从上一个值更新您的计数器。初始化时间

进行时间变量类级别

最新更新