diff --git a/model_zoo/official/lite/MindSpore_inhand/dance/src/main/java/com/mindspore/dance/view/DanceActivity.java b/model_zoo/official/lite/MindSpore_inhand/dance/src/main/java/com/mindspore/dance/view/DanceActivity.java index e2d6c15f6f1ea3fd36f473d81cf629ab356fca2b..faa5d48dd1c480003973ac9021e349a08fa71d50 100644 --- a/model_zoo/official/lite/MindSpore_inhand/dance/src/main/java/com/mindspore/dance/view/DanceActivity.java +++ b/model_zoo/official/lite/MindSpore_inhand/dance/src/main/java/com/mindspore/dance/view/DanceActivity.java @@ -48,4 +48,9 @@ public class DanceActivity extends AppCompatActivity { } + @Override + public void onBackPressed() { + finish(); + } + } diff --git a/model_zoo/official/lite/MindSpore_inhand/dance/src/main/java/com/mindspore/dance/view/fragment/RunFragment.java b/model_zoo/official/lite/MindSpore_inhand/dance/src/main/java/com/mindspore/dance/view/fragment/RunFragment.java index a51d1361b4702d632a0854952e41a11b0c85e912..d41b84c8abbff9ed4393ed20abd3ec230f91255e 100644 --- a/model_zoo/official/lite/MindSpore_inhand/dance/src/main/java/com/mindspore/dance/view/fragment/RunFragment.java +++ b/model_zoo/official/lite/MindSpore_inhand/dance/src/main/java/com/mindspore/dance/view/fragment/RunFragment.java @@ -18,9 +18,9 @@ package com.mindspore.dance.view.fragment; import android.annotation.SuppressLint; import android.media.MediaPlayer; -import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.os.Message; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; @@ -33,11 +33,8 @@ import android.widget.MediaController; import android.widget.TextView; import androidx.annotation.NonNull; -import androidx.annotation.RequiresApi; -import androidx.camera.camera2.Camera2Config; import androidx.camera.core.Camera; import androidx.camera.core.CameraSelector; -import androidx.camera.core.CameraXConfig; import androidx.camera.core.Preview; import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.camera.view.PreviewView; @@ -53,9 +50,15 @@ import com.mindspore.dance.present.video.MyVideoView; import com.mindspore.dance.task.SampleTask; import com.mindspore.dance.util.Tools; +import java.lang.ref.WeakReference; +import java.util.Timer; +import java.util.TimerTask; import java.util.concurrent.ExecutionException; public class RunFragment extends Fragment { + private final static int COUNT_DOWN_1 = 1; + private final static int COUNT_DOWN_2 = 2; + private final static int COUNT_DOWN_3 = 3; private final String TAG = RunFragment.class.getSimpleName(); private FrameLayout root; private MyVideoView videoView; @@ -64,9 +67,10 @@ public class RunFragment extends Fragment { private SampleTask sampleTask; private TextView countdownView; private ImageView info; - private Handler handler; - private final int NEED_CANCEL = -1; private MediaController mediaController; + private int timeWhat; + private Handler mHandler; + private Timer timer; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -77,7 +81,7 @@ public class RunFragment extends Fragment { public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); root = view.findViewById(R.id.root); - handler = new Handler(); + mHandler = new MyHandler(this); mediaController = new MediaController(this.getContext()); initView(); @@ -99,27 +103,70 @@ public class RunFragment extends Fragment { } } - @RequiresApi(api = Build.VERSION_CODES.P) + private void setTimer() { + timer.schedule(new TimerTask() { + @Override + public void run() { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + Log.d(TAG, "run this thread:"+android.os.Process.myTid()); + Message message = new Message(); + message.what = timeWhat; + timeWhat++; + mHandler.sendMessage(message); + } + }); + thread.start(); + } + }, 1000, 1000); + } + + private static class MyHandler extends Handler { + + private WeakReference weakReference; + + public MyHandler(RunFragment fragment) { + weakReference = new WeakReference<>(fragment); + } + + public void handleMessage(Message msg) { + RunFragment thisFragment = this.weakReference.get(); + switch (msg.what) { + case COUNT_DOWN_1: + thisFragment.info.setVisibility(View.VISIBLE); + thisFragment.info.setBackgroundResource(R.drawable.countdown_two); + break; + case COUNT_DOWN_2: + thisFragment.info.setVisibility(View.VISIBLE); + thisFragment.info.setBackgroundResource(R.drawable.countdown_one); + break; + case COUNT_DOWN_3: + thisFragment.beginPlay(); + thisFragment.info.setVisibility(View.GONE); + break; + default: + thisFragment.timer.cancel(); + break; + } + super.handleMessage(msg); + } + } + @Override public void onResume() { + super.onResume(); info.setVisibility(View.VISIBLE); info.setBackgroundResource(R.drawable.countdown_three); - super.onResume(); - handler.postDelayed(() -> { - //execute the task - info.setVisibility(View.VISIBLE); - info.setBackgroundResource(R.drawable.countdown_two); - }, NEED_CANCEL, 1000); - handler.postDelayed(() -> { - //execute the task - info.setVisibility(View.VISIBLE); - info.setBackgroundResource(R.drawable.countdown_one); - }, NEED_CANCEL, 2000); - handler.postDelayed(() -> { - //execute the task - beginPlay(); - info.setVisibility(View.GONE); - }, NEED_CANCEL, 3080); + timer = new Timer(); + timeWhat = COUNT_DOWN_1; + setTimer(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + mHandler = null; } private void start() { @@ -142,7 +189,7 @@ public class RunFragment extends Fragment { @Override public void onPause() { super.onPause(); - handler.removeMessages(NEED_CANCEL); + timer.cancel(); if (sampleTask != null) { sampleTask.setNeedStop(true); sampleTask.clear(); @@ -225,6 +272,6 @@ public class RunFragment extends Fragment { backButton.setOnClickListener(view -> { getActivity().finish(); }); - Tools.addView(getActivity(), root, backButton, 0.1f, 0.2f, 0.12f, 0.1f); + Tools.addView(getActivity(), root, backButton, 0.1f, 0.2f, 0.20f, 0.1f); } }