我正在研究GIF解码器。该代码工作正常。只觉得我对kotlin完全陌生,所以无法理解其中使用的语法。所以我完全迷失了。帮我用Java编写同样的代码。我希望此代码可用于主要活动(Java)。但是当前的代码是为Kotlin中的Fragments编写的。
该库使用了https://github.com/koral--/android-gif-drawable
代码:
class GifDecoderFragment : BaseFragment(), CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
private var frames = emptyList<Bitmap>()
private var durations = emptyList<Int>()
private var currentFrameIndex = 0
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.decoder, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
launch(Dispatchers.IO) {
val frames = mutableListOf<Bitmap>()
val durations = mutableListOf<Int>()
val decoder = GifDecoder(InputSource.ResourcesSource(resources, R.drawable.room3))
for (i in 0 until decoder.numberOfFrames) {
val frame = Bitmap.createBitmap(decoder.width, decoder.height, Bitmap.Config.ARGB_8888)
decoder.seekToFrame(i, frame)
Log.d("BaseActivityneww", "onCreate: $i")
frames += frame
durations += decoder.getFrameDuration(i)
}
decoder.recycle()
withContext(Dispatchers.Main){
this@GifDecoderFragment.frames = frames
this@GifDecoderFragment.durations = durations
if (isAdded) {
startAnimation()
decoderLoadingTextView.visibility = View.GONE
}
}
}
}
override fun onResume() {
super.onResume()
if (frames.isNotEmpty()) {
startAnimation()
}
}
private fun startAnimation() {
decoderImageView.setImageBitmap(frames[currentFrameIndex])
launch {
delay(durations[currentFrameIndex].toLong())
advanceAnimation()
}
}
override fun onPause() {
job.cancelChildren()
super.onPause()
}
override fun onDestroy() {
job.cancel()
super.onDestroy()
}
private fun advanceAnimation() {
currentFrameIndex++
currentFrameIndex %= frames.size
decoderImageView.setImageBitmap(frames[currentFrameIndex])
launch {
delay(durations[currentFrameIndex].toLong())
advanceAnimation()
}
}
Xml:
<ImageView
android:id="@+id/decoderImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
答案 0 :(得分:1)
如果您使用的是Android Studio,则可以使用The Kotlin插件:
菜单>工具> Kotlin->将Kotlin反编译为Java。