Просмотр исходного кода

重构卡路里和CK计算

Signed-off-by: duanchangpeng <838560574@qq.com>
duanchangpeng 4 лет назад
Родитель
Сommit
eafb093bb5

+ 31 - 0
app/src/main/java/com/example/watch/ui/activity/Animate.kt

@@ -0,0 +1,31 @@
+package com.example.watch.ui.activity
+
+import android.view.animation.Animation
+import android.view.animation.DecelerateInterpolator
+import android.view.animation.ScaleAnimation
+
+class Animate {
+    fun getScaleAnimation(heartRate:Int): Animation {
+        //实例化 ScaleAnimation 主要是缩放效果
+        //参数:fromX-动画开始前,x坐标   toX-动画结束后x坐标
+        //fromY-动画开始前,Y坐标  toY-动画结束后Y坐标
+        //pivotXType - 为动画相对于物件的X坐标的参照物   pivotXValue - 值
+        //pivotYType - 为动画相对于物件的Y坐标的参照物   pivotYValue - 值
+        val animation: Animation = ScaleAnimation(
+            1.0f, 0.6f, 1.0f, 0.6f,
+            Animation.RELATIVE_TO_SELF, 0.5f,
+            Animation.RELATIVE_TO_SELF, 0.5f
+        )
+        //设置动画插值器 被用来修饰动画效果,定义动画的变化率
+        animation.interpolator = DecelerateInterpolator()
+        //设置动画执行时间
+        if(heartRate > 100){
+            animation.duration = 1000
+        }else{
+            animation.duration = 3000
+        }
+
+        animation.repeatCount = Animation.INFINITE
+        return animation
+    }
+}

+ 20 - 6
app/src/main/java/com/example/watch/ui/activity/BaseData.kt

@@ -13,7 +13,14 @@ class BaseData {
     val peaceHr = 70
 
     //  运动强度计算
-    public fun calcActivity(Hr: Int, Sex: Int, Weight: Int, Age: Int, T: Int): String {
+    fun clacActLevel(Hr: Int): String {
+        val sum: Int = 170 - age
+        var actLevel: Double = ((Hr.toDouble() / sum.toDouble()) * 100)
+        val actLevelText = actLevel.toInt().toString()
+        return actLevelText
+    }
+
+    public fun calcActivity(Hr: Int, Sex: Int, Weight: Int, Age: Int, T: Double): String {
         var calc: Double
         if (Sex == 1) {
             calc =
@@ -55,12 +62,19 @@ class BaseData {
 
     //    todo 计算CK
     public fun calcCk(Hr: Int): String {
-//        卡路里除以体重
-        var res = "0"
-        val calc = Hr.toDouble() / weight
-        if (calc > 1) {
-            res = calc.toInt().toString()
+        var calc: Double
+        if (sex == 1) {
+            calc =
+                (((-55.0969 + (0.6309 * Hr) + (0.1988 * weight) + (0.2017 * age)) / 4.184) * 60 * 1)
+        } else {
+            calc =
+                (((-20.4022 + (0.4472 * Hr) + (0.1263 * weight) + (0.074 * age)) / 4.184) * 60 * 1)
         }
+//        强度最大100
+        calc = if (calc >= 100.0) 100.0 else calc
+        calc = if (calc <= 0.0) 0.0 else calc
+        println(calc)
+        val res = calc.toInt().toString()
         return res
     }
 }

+ 87 - 0
app/src/main/java/com/example/watch/ui/activity/EnterActivity.kt

@@ -0,0 +1,87 @@
+package com.example.watch.ui.activity
+
+import android.os.Bundle
+import android.view.MenuItem
+import androidx.appcompat.app.AppCompatActivity
+import androidx.fragment.app.Fragment
+import com.example.watch.ui.activity.fragment.HomeFragment
+import com.google.android.material.bottomnavigation.BottomNavigationView
+import com.example.watch.R
+
+class EnterActivity : AppCompatActivity() {
+    private val mFragments = arrayOfNulls<Fragment>(1)
+    private var mBottomNav: BottomNavigationView? = null
+    private var mPreFragmentFlag = 0
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setContentView(R.layout.activity_main)
+        initView()
+        initFragment()
+        selectFragment()
+    }
+
+    private fun initView() {
+        mBottomNav = findViewById(R.id.bottomNavigation)
+    }
+
+    private fun initFragment() {
+        mFragments[0] = HomeFragment()
+//        mFragments[1] = WeChatFragment()
+//        mFragments[2] = ProjectFragment()
+//        mFragments[3] = SystemFragment()
+//        mFragments[4] = SettingFragment()
+        initLoadFragment(R.id.mContainerView, 0, mFragments)
+    }
+
+    // 参数一 是一个FrameLayout的ID,用来动态加载Fragment,
+    private fun initLoadFragment(containerId: Int, showFragment: Int, fragments: Array<Fragment?>) {
+        //获取到FragmentManager实例的同时去开启事物
+        val transaction = supportFragmentManager.beginTransaction()
+        transaction.add(containerId,  HomeFragment(), fragments[0]!!.javaClass.name)
+//        for (i in 0 until fragments.size) {
+//            //首先将Fragment添加到事务中
+//            transaction.add(containerId, fragments[i]!!, fragments[i]!!.javaClass.name)
+//            //默认展示 fragments[showFragment]
+//            //这里做首次Fragment的展示,如果不是指定的Fragment就先隐藏,需要的时候再显示出来
+//            if (i != showFragment) transaction.hide(fragments[i]!!)
+//        }
+        //提交事物
+        transaction.commitAllowingStateLoss()
+    }
+
+    private fun selectFragment() {
+        //注册监听事件
+        mBottomNav!!.itemIconTintList = null
+        mBottomNav!!.setOnNavigationItemSelectedListener { menuItem: MenuItem ->
+            when (menuItem.itemId) {
+                R.id.home -> {
+                    showAndHideFragment(mFragments[0], mFragments[mPreFragmentFlag])
+                    mPreFragmentFlag = 0
+                }
+//                R.id.wechat -> {
+//                    showAndHideFragment(mFragments[1], mFragments[mPreFragmentFlag])
+//                    mPreFragmentFlag = 1
+//                }
+//                R.id.project -> {
+//                    showAndHideFragment(mFragments[2], mFragments[mPreFragmentFlag])
+//                    mPreFragmentFlag = 2
+//                }
+//                R.id.system -> {
+//                    showAndHideFragment(mFragments[3], mFragments[mPreFragmentFlag])
+//                    mPreFragmentFlag = 3
+//                }
+//                R.id.setting -> {
+//                    showAndHideFragment(mFragments[4], mFragments[mPreFragmentFlag])
+//                    mPreFragmentFlag = 4
+//                }
+            }
+            true
+        }
+    }
+
+    //加载不同的Fragment
+    private fun showAndHideFragment(show: Fragment?, hide: Fragment?) {
+        val transaction = supportFragmentManager.beginTransaction()
+        if (show !== hide) transaction.show(show!!).hide(hide!!).commitAllowingStateLoss()
+    }
+}

+ 14 - 32
app/src/main/java/com/example/watch/ui/activity/MainActivity.kt

@@ -18,6 +18,7 @@ import android.view.KeyEvent
 import android.view.View
 import android.view.WindowManager
 import android.view.animation.Animation
+import android.view.animation.DecelerateInterpolator
 import android.view.animation.ScaleAnimation
 import android.widget.*
 import androidx.appcompat.app.AlertDialog
@@ -56,10 +57,9 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
     private val viewPager: ViewPager? = null
     private val bottomNavigationView: BottomNavigationView? = null
     private val mPagerAdapter: FragmentPagerAdapter? = null
-
     var toast: Toast? = null
     var mExitTime: Long = 0
-    var HeartLine = arrayListOf<Any>(0, 0, 0)
+    var HeartLine = arrayListOf<Any>()
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -85,25 +85,6 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
 //        绘制心律图
         ChartView()
 
-        //        心跳动画根据心跳数进行加载 todo 获取心率数据后跑不动
-        heartAnimate()
-    }
-
-    fun heartAnimate() {
-        val mIdIvFace = findViewById<ImageView>(R.id.heart)
-        val scaleAnimation = ScaleAnimation(
-            0.6f, 1f, 0.6f, 1f,
-            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
-        )
-        // 设置动画持续时间
-        scaleAnimation.duration = 1000
-
-        // 为ImageView设置动画效果
-        mIdIvFace.setAnimation(scaleAnimation)
-        // 启动动画
-//        scaleAnimation.startNow()
-        scaleAnimation.repeatCount = Animation.INFINITE
-        scaleAnimation.start()
     }
 
     fun setStatusBarColor(colorType: String) {
@@ -123,8 +104,8 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
 
     private fun linkHeartBand() {
         val chainBtn = findViewById<Button>(R.id.chainBtn)
-        chainBtn.setOnClickListener {
 
+        chainBtn.setOnClickListener {
             if (isBLEEnabled()) {
                 if (!mDeviceConnected) {
                     showDeviceScanningDialog()
@@ -365,8 +346,6 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
     override fun onSportReceived(device: BluetoothDevice, step: Int, distance: Int, calorie: Int) {
         findViewById<TextView>(R.id.sportNum).text = calorie.toString()
         findViewById<TextView>(R.id.calNum).text = (calorie * 1.25).toInt().toString()
-        findViewById<TextView>(R.id.ckNum).text = BaseData().calcCk(calorie)
-
     }
 
     //    连续两次回退
@@ -401,6 +380,10 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
 //        心率带已链接
         bindHeartSuccess()
 
+        //        心跳动画根据心跳数进行加载,大于100速度快
+        val scalegetAnim: Animation = Animate().getScaleAnimation(heartRate)
+        findViewById<TextView>(R.id.heartReal).startAnimation(scalegetAnim)
+
         //心跳
         findViewById<TextView>(R.id.heartReal).text = heartRate.toString()
         if (rrIntervals != null) {
@@ -423,16 +406,15 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
             )
         )
 
+//        计算CK
+        findViewById<TextView>(R.id.ckNum).text = BaseData().calcCk(heartRate)
+
 //        todo 最大心率 和 平均心率
 //        val arrayList = ArrayList<Int>()
 
-
-//        运动强度
-//    男:((-55.0969 +(0.6309×HR)+(0.1988 xW)+(0.2017×A))/ 4.184)×60 xT
-//    女:((-20.4022 +(0.4472×HR) - (0.1263 xW)+(0.074×A))/ 4.184)×60 xT
-//    其中,HR =心率(次/分钟) W =体重(公斤) A=年龄(岁) T =锻炼持续时间的时间(以小时计)
-        var ActityVal =
-            BaseData().calcActivity(heartRate, BaseData().sex, BaseData().weight, BaseData().age, 1)
+//        var ActityVal =
+//            BaseData().calcActivity(heartRate, BaseData().sex, BaseData().weight, BaseData().age, 0.8)
+        var ActityVal = BaseData().clacActLevel(heartRate)
         this.findViewById<TextView>(R.id.activLevel).text = ActityVal
 
 
@@ -533,10 +515,10 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
 
     fun ChartView() {
         val aaChartView = findViewById<AAChartView>(R.id.aa_chart_view)
-//        arrayOf(0, 0)
         val aaChartModel = AAChartModel()
             .chartType(AAChartType.Line)
             .backgroundColor("#fff")
+            .colorsTheme(arrayOf("#F90505"))
             .series(
                 arrayOf(
                     AASeriesElement()

+ 0 - 2
app/src/main/java/com/example/watch/ui/activity/OneActivity.kt

@@ -67,7 +67,5 @@ class OneActivity : AppCompatActivity() {
             1 -> window.statusBarColor = resources.getColor(R.color.yellow)
             2 -> window.statusBarColor = resources.getColor(R.color.green)
         }
-
     }
-
 }

+ 13 - 5
app/src/main/java/com/example/watch/ui/activity/SettingActivity.kt

@@ -4,13 +4,14 @@ import android.content.Intent
 import android.os.Bundle
 import android.view.View
 import android.view.WindowManager
-import android.widget.Button
-import android.widget.LinearLayout
-import android.widget.TextView
-import android.widget.Toast
+import android.view.animation.Animation
+import android.view.animation.DecelerateInterpolator
+import android.view.animation.ScaleAnimation
+import android.widget.*
 import androidx.appcompat.app.AppCompatActivity
 import com.example.watch.R
 
+
 class SettingActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -28,8 +29,15 @@ class SettingActivity : AppCompatActivity() {
 //        推出登录
         logout()
 
-    }
+//        val imageView: ImageView = findViewById<View>(R.id.imageView) as ImageView
+//        //图片点击的时候,启动动画效果
+//        //图片点击的时候,启动动画效果
+//        imageView.setOnClickListener({
+//            val scalegetAnim: Animation = Animate().getScaleAnimation()
+//            imageView.startAnimation(scalegetAnim)
+//        })
 
+    }
     private fun logout() {
         findViewById<Button>(R.id.logout).setOnClickListener {
             Toast.makeText(this, "当前账号已退出", Toast.LENGTH_LONG).show()

+ 60 - 0
app/src/main/java/com/example/watch/ui/activity/fragment/HomeFragment.kt

@@ -0,0 +1,60 @@
+package com.example.watch.ui.activity.fragment
+
+import android.os.Bundle
+import androidx.fragment.app.Fragment
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import com.example.watch.R
+
+// TODO: Rename parameter arguments, choose names that match
+// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
+private const val ARG_PARAM1 = "param1"
+private const val ARG_PARAM2 = "param2"
+
+/**
+ * A simple [Fragment] subclass.
+ * Use the [HomeFragment.newInstance] factory method to
+ * create an instance of this fragment.
+ */
+class HomeFragment : Fragment() {
+    // TODO: Rename and change types of parameters
+    private var param1: String? = null
+    private var param2: String? = null
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        arguments?.let {
+            param1 = it.getString(ARG_PARAM1)
+            param2 = it.getString(ARG_PARAM2)
+        }
+    }
+
+    override fun onCreateView(
+        inflater: LayoutInflater, container: ViewGroup?,
+        savedInstanceState: Bundle?
+    ): View? {
+        // Inflate the layout for this fragment
+        return inflater.inflate(R.layout.fragment_home, container, false)
+    }
+
+    companion object {
+        /**
+         * Use this factory method to create a new instance of
+         * this fragment using the provided parameters.
+         *
+         * @param param1 Parameter 1.
+         * @param param2 Parameter 2.
+         * @return A new instance of fragment HomeFragment.
+         */
+        // TODO: Rename and change types and number of parameters
+        @JvmStatic
+        fun newInstance(param1: String, param2: String) =
+            HomeFragment().apply {
+                arguments = Bundle().apply {
+                    putString(ARG_PARAM1, param1)
+                    putString(ARG_PARAM2, param2)
+                }
+            }
+    }
+}

+ 60 - 0
app/src/main/java/com/example/watch/ui/activity/fragment/SettingsFragment.kt

@@ -0,0 +1,60 @@
+package com.example.watch.ui.activity.fragment
+
+import android.os.Bundle
+import androidx.fragment.app.Fragment
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import com.example.watch.R
+
+// TODO: Rename parameter arguments, choose names that match
+// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
+private const val ARG_PARAM1 = "param1"
+private const val ARG_PARAM2 = "param2"
+
+/**
+ * A simple [Fragment] subclass.
+ * Use the [SettingsFragment.newInstance] factory method to
+ * create an instance of this fragment.
+ */
+class SettingsFragment : Fragment() {
+    // TODO: Rename and change types of parameters
+    private var param1: String? = null
+    private var param2: String? = null
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        arguments?.let {
+            param1 = it.getString(ARG_PARAM1)
+            param2 = it.getString(ARG_PARAM2)
+        }
+    }
+
+    override fun onCreateView(
+        inflater: LayoutInflater, container: ViewGroup?,
+        savedInstanceState: Bundle?
+    ): View? {
+        // Inflate the layout for this fragment
+        return inflater.inflate(R.layout.fragment_settings, container, false)
+    }
+
+    companion object {
+        /**
+         * Use this factory method to create a new instance of
+         * this fragment using the provided parameters.
+         *
+         * @param param1 Parameter 1.
+         * @param param2 Parameter 2.
+         * @return A new instance of fragment SettingsFragment.
+         */
+        // TODO: Rename and change types and number of parameters
+        @JvmStatic
+        fun newInstance(param1: String, param2: String) =
+            SettingsFragment().apply {
+                arguments = Bundle().apply {
+                    putString(ARG_PARAM1, param1)
+                    putString(ARG_PARAM2, param2)
+                }
+            }
+    }
+}

+ 31 - 0
app/src/main/res/layout/activity_entery.xml

@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.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=".ui.activity.EnterActivity"
+    tools:ignore="MissingConstraints">
+
+    <FrameLayout
+        android:id="@+id/mContainerView"
+        app:layout_constraintTop_toTopOf="parent"
+        app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
+        android:layout_width="match_parent"
+        android:layout_height="0dp"/>
+
+
+    <com.google.android.material.bottomnavigation.BottomNavigationView
+        android:id="@+id/bottomNavigation"
+        android:layout_width="0dp"
+        android:layout_height="50dp"
+        android:background="#FCFCFC"
+        android:paddingTop="0dp"
+        app:itemIconSize="20dp"
+        app:labelVisibilityMode="labeled"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:menu="@menu/bottom_nav_menu" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 2 - 1
app/src/main/res/layout/activity_main.xml

@@ -31,7 +31,7 @@
             android:layout_height="match_parent"
             android:layout_centerHorizontal="true"
             android:layout_weight="1"
-            android:layout_marginTop="20dp"
+            android:layout_marginTop="3dp"
             layout="@layout/main_chart_part" />
     </LinearLayout>
 
@@ -46,5 +46,6 @@
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
+        app:itemTextColor="@color/black"
         app:menu="@menu/bottom_nav_menu" />
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 14 - 0
app/src/main/res/layout/fragment_home.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".ui.activity.fragment.HomeFragment">
+
+    <!-- TODO: Update blank fragment layout -->
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:text="@string/hello_blank_fragment" />
+
+</FrameLayout>

+ 14 - 0
app/src/main/res/layout/fragment_setting.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".ui.activity.fragment.setting">
+
+    <!-- TODO: Update blank fragment layout -->
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:text="@string/hello_blank_fragment" />
+
+</FrameLayout>

+ 14 - 0
app/src/main/res/layout/fragment_settings.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".ui.activity.fragment.SettingsFragment">
+
+    <!-- TODO: Update blank fragment layout -->
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:text="@string/hello_blank_fragment" />
+
+</FrameLayout>

+ 18 - 0
app/src/main/res/layout/home_fragment.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    tools:context=".fragment.HomeFragment">
+
+    <TextView
+        app:layout_constraintTop_toTopOf="parent"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="首页" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>