我有一个kotlin Gif decorder代码,但我希望这个代码是用java编写的
我在做GIF解码器。代码运行良好。我只是觉得我对kotlin完全陌生,所以无法理解中使用的语法。所以我完全迷路了。请帮助我用Java编写相同的代码。我希望在主活动(Java)中使用此代码。但是当前的代码是为Kotlin中的片段编写的 这个图书馆 代码:我有一个kotlin Gif decorder代码,但我希望这个代码是用java编写的,java,android,xml,kotlin,gif,Java,Android,Xml,Kotlin,Gif,我在做GIF解码器。代码运行良好。我只是觉得我对kotlin完全陌生,所以无法理解中使用的语法。所以我完全迷路了。请帮助我用Java编写相同的代码。我希望在主活动(Java)中使用此代码。但是当前的代码是为Kotlin中的片段编写的 这个图书馆 代码: 类GifDecoderFragment:BaseFragment(),CoroutineScope{ private val job=job() 覆盖val coroutineContext:coroutineContext get()=调度程序
类GifDecoderFragment:BaseFragment(),CoroutineScope{
private val job=job()
覆盖val coroutineContext:coroutineContext
get()=调度程序.Main+作业
私有变量frames=emptyList()
private var durations=emptyList()
私有变量currentFrameIndex=0
覆盖创建视图(充气机:布局充气机,容器:ViewGroup?,savedInstanceState:Bundle?):视图{
返回充气机。充气(右布局解码器,容器,假)
}
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
发射(Dispatchers.IO){
val frames=mutableListOf()
val durations=mutableListOf()
val decoder=GIFDecker(InputSource.ResourcesSource(resources,R.drawable.room3))
for(直到解码器.numberOfFrames之前的0中的i){
val frame=Bitmap.createBitmap(decoder.width、decoder.height、Bitmap.Config.ARGB_8888)
解码器。seekToFrame(i,帧)
Log.d(“BaseActivityneww”,“onCreate:$i”)
帧+=帧
durations+=解码器.getFrameDuration(i)
}
decoder.recycle()
withContext(Dispatchers.Main){
this@GifDecoderFragment.frames=帧
this@GifDecoderFragment.durations=持续时间
如果(已添加){
startAnimation()
decoderLoadingTextView.visibility=View.GONE
}
}
}
}
重写onResume(){
super.onResume()
if(frames.isNotEmpty()){
startAnimation()
}
}
私人娱乐开始活动(){
decoderImageView.setImageBitmap(帧[currentFrameIndex])
发射{
延迟(持续时间[currentFrameIndex].toLong())
高级化
}
}
覆盖暂停(){
job.cancelChildren()
super.onPause()
}
重写onDestroy(){
作业。取消()
super.ondestory()
}
私人娱乐高级活动(){
当前帧索引++
currentFrameIndex%=frames.size
decoderImageView.setImageBitmap(帧[currentFrameIndex])
发射{
延迟(持续时间[currentFrameIndex].toLong())
高级化
}
}
Xml:
如果您使用的是Android Studio,则可以使用Kotlin插件: 菜单>工具>Kotlin->将Kotlin反编译为Java
这段代码使用的是协同路由,这是kotlin特有的特性,因此您将无法在JavaOoh中编写完全相同的行为……还有其他选项可以这样做吗?根据stackoverflow的另一个答案,kotlin编译器似乎对协同路由执行了某种转换,所以我想在JavaOoh中没有简单的方法可以做到同样的事情k、 …但是使用的主要库是用java编写的。这个链接是ohk bro谢谢…我会给你一个tryyaa我已经更新到最新的插件…但是它仍然被禁用。你的Android Studio版本是什么?Android Studio版本是3.3.1请参考这个链接blog.safedk.com/technology/hello-kotlin-convert-Android-project-part-1/这个din帮助我
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()
}
}
<ImageView
android:id="@+id/decoderImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>