网站首页 > 博客文章 正文
前篇文章讲解了关于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图片暂时和一个文字展示。
好了,今天就介绍在这里了,欢迎关注,点赞,转发,后续我们继续开发
猜你喜欢
- 2024-09-29 直击灵魂一问:协程到底是怎么切换线程的?
- 2024-09-29 八月冲刺别毁在传统一问一答模式,这份面试准备助你事半功倍
- 2024-09-29 Kotlin遇见数据结构丨说说顺序存储的二叉树如何创建遍历
- 2024-09-29 Kotlin实战—使用Room封装本地数据层
- 2024-09-29 知了堂|MybatisPlus—kotlin代码生成
- 2024-09-29 竟然有5种微服务框架可以替代Spring Boot
- 2024-09-29 ViewBinding 与 Kotlin 委托双剑合璧
- 2024-09-29 Kotlin+SpringBoot+Redis+Lua实现限流访问控制详解
- 2024-09-29 北上广程序员回重庆继续代码 kotlin音视频解析六 收藏
- 2024-09-29 字节谷歌Kotlin学习王炸!入门到精通+高级强化实战-源码
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)