美文网首页
android -属性动画的应用 旋转的专辑封面

android -属性动画的应用 旋转的专辑封面

作者: 大丸蛇 | 来源:发表于2019-07-26 13:20 被阅读0次
    class MainActivity : AppCompatActivity() {
    
        private var timer: Timer? = null
    
        private var task: TimerTask? = null
    
        var isPause = true
    
        private var i = 0.0f
    
        private lateinit var animator: ObjectAnimator
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            iv_rotate.setImageResource(R.mipmap.ic_launcher_round)
    
            animator  = ObjectAnimator.ofFloat(iv_rotate, "rotation", 0.0f, 360.0f)
    
            with(animator) {
                duration = 10*1000
                interpolator = LinearInterpolator()
                repeatCount = -1
                repeatMode = ObjectAnimator.RESTART
            }
    
            btn_start.setOnClickListener {
                startTimer()
            }
    
            btn_stop.setOnClickListener {
                stopTimer()
            }
    
            btn_anim_reset.setOnClickListener {
    
                animator.end()
            }
    
            btn_anim_start.setOnClickListener {
    
                animator.start()
            }
    
            btn_anim_resume.setOnClickListener {
    
                animator.resume()
            }
            btn_anim_stop.setOnClickListener {
    
                animator.pause()
            }
    
        }
    
        private fun startTimer() {
            if (isPause) {
                if (timer == null) {
                    timer = Timer()
                }
    
                if (task == null) {
                    task = object : TimerTask() {
                        override fun run() {
                            runOnUiThread {
                                i += 0.2f
                                iv_rotate.rotation = i % 360f
                            }
    
                        }
                    }
                }
    
                timer?.schedule(task, 5, 5)
                isPause = false
            }
        }
    
        private fun stopTimer() {
            if (!isPause) {
                timer?.cancel()
                timer = null
                task?.cancel()
                task = null
                isPause = true
    
            }
        }
    
    
    }
    
    <?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=".MainActivity">
    
        <ImageView
                android:layout_marginStart="168dp"
                android:layout_marginBottom="312dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:id="@+id/iv_rotate"
                android:layout_width="80dp"
                android:layout_height="80dp"
                tools:ignore="ContentDescription"/>
    
    
        <Button
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:id="@+id/btn_start"
                android:text="@string/start_rotate_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_marginStart="16dp"/>
    
        <Button
                app:layout_constraintStart_toEndOf="@id/btn_start"
                app:layout_constraintBottom_toBottomOf="parent"
                android:id="@+id/btn_stop"
                android:text="@string/stop_rotate_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_marginStart="128dp"/>
    
    
        <Button
                android:textSize="12sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:id="@+id/btn_anim_start"
                android:text="@string/start_anim"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
        <Button
                android:textSize="12sp"
                app:layout_constraintStart_toEndOf="@id/btn_anim_start"
                app:layout_constraintTop_toTopOf="parent"
                android:id="@+id/btn_anim_stop"
                android:text="@string/stop_anim"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <Button
    
                android:textSize="12sp"
                app:layout_constraintStart_toEndOf="@id/btn_anim_stop"
                app:layout_constraintTop_toTopOf="parent"
                android:id="@+id/btn_anim_reset"
                android:text="@string/reset_anim"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
        <Button
                android:textSize="12sp"
                app:layout_constraintStart_toEndOf="@id/btn_anim_reset"
                app:layout_constraintTop_toTopOf="parent"
                android:id="@+id/btn_anim_resume"
                android:text="@string/resume_anim"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    相关文章

      网友评论

          本文标题:android -属性动画的应用 旋转的专辑封面

          本文链接:https://www.haomeiwen.com/subject/qsdcrctx.html