专业的编程技术博客社区

网站首页 > 博客文章 正文

Kotlin编写一个GIFDialog 在线音视频解析三

baijin 2024-09-29 08:50:29 博客文章 6 ℃ 0 评论

前篇文章讲解了关于kotlin编写的一些回调方法和一些对话框,今天这篇文章也是讲解对话框的代码,因为这些都是这个项目的前期需要准备的,所以我就讲解一下。


做技术每天都是在不停的忙碌和学习当中,像kotlin也是一门不新不旧的语言,但是google大量宣传的作用下,也是有很多公司在使用了,所以作为安卓开发人员不得不学习它。


也是在学习当中才想到这个项目的,学以致用才是真正的学习,学程序员最好的方式就是不停的写代码,写项目,在写项目和写代码中成长。


今天要讲解的就是GIFDialog 这个对话框用kotlin怎么写。

DialogFragment 这个东东做安卓的已经不陌生了,只要写安卓项目都会用到的一个对话框类。

class GIFDialog : DialogFragment() {

    companion object {
        val TAG = GIFDialog::javaClass.name

        lateinit var file: File

        fun show(fragmentManager: FragmentManager, file: File) {
            this.file = file
            GIFDialog().show(fragmentManager, TAG)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        val view = activity!!.layoutInflater.inflate(R.layout.dialog_gif_preview, null)

        val gifView = view.findViewById<GifImageView>(R.id.gif_view)
        val videoInfo = view.findViewById<AppCompatTextView>(R.id.video_info)

        val gif = GifDrawable(file.path)
        gifView.setImageDrawable(gif)
        gif.start()

        videoInfo.text = "Duration: ${Utils.milliSecondsToTimer(gif.duration.toLong())}" + " Frames: ${gif.numberOfFrames}"

        return AlertDialog.Builder(activity)
                .setView(view)
                .setTitle("Preview")
                .setPositiveButton("Cancel") { dialog, which ->
                    dismiss()
                }
                .create()
    }
}

上面代码分析 show 方法大家看的懂吧,这个里面用到一个companion 类内部的对象声明可以用 companion 关键字标记,这样它就与外部类关联在一起,我们就可以直接通过外部类访问到对象的内部元素。

其实就是java当中的静态方法类一个道理。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <pl.droidsonroids.gif.GifImageView
        android:id="@+id/gif_view"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_margin="10dp" />

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/video_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#000000"
        android:textSize="16sp" />

</LinearLayout>

布局文件也很简单,就是一个GIF图片暂时和一个文字展示。


好了,今天就介绍在这里了,欢迎关注,点赞,转发,后续我们继续开发

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表