It is important to note that the JSON animation should only be kept in a folder named raw under the res folder since the functions are made explicitly to call this file path. And the name of the JSON file should not contain any special character other then ‘_‘.
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring the buttons
val startBtn = findViewById
步骤5:使用MainActivity.kt文件
下面是MainActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。
科特林
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring the buttons
val startBtn = findViewById(R.id.btnStart)
val stopBtn = findViewById(R.id.btnStop)
// On the click of startBtn
startBtn.setOnClickListener {
// is an Animation Listener
animationView.playAnimation()
}
// On the click of stopBtn
stopBtn.setOnClickListener {
// is an Animation Listener
animationView.pauseAnimation()
}
}
}