
安卓
安卓相关
源文雨
千载何方归梦,棹舟星河波开。
展开
-
安卓Glide加载失败时点击按钮重新加载图片
当Glide请求遇到网络突然中断导致图片加载失败时,想要通过点击一个Button重新加载。原创 2024-04-02 01:17:40 · 1042 阅读 · 0 评论 -
安卓沉浸状态栏下 PreferenceFragment 弹出的输入对话框无法跟随键盘上移的解决办法
在不沉浸状态栏时,点击呼出的输入对话框将跟随键盘上移,但一旦沉浸状态栏或导航栏后,再呼出对话框时则无法上移。原创 2023-11-03 13:40:12 · 633 阅读 · 0 评论 -
安卓 利用Termux 实现批量导入单话本到 Tachiyomi 本地漫画目录
问题分析因为Tachiyomi默认一个漫画有很多话,所以强制采用了两层目录结构。对于单话本,需要将对应的zip放到./书名/xxx.zip才能识别,在需要转移的本数很大时,手工操作就显得十分艰难。针对未解压的zip实现该脚本将入参目录下所有zip放入$TACHILOCAL/zip名去除空格/chapter.zip下。之所以要去除空格,是因为Tachiyomi无法加载有空格的文件夹的封面。#!/bin/shTACHILOCAL="/storage/emulated/0/Tachiyomi原创 2021-08-02 15:43:46 · 7108 阅读 · 0 评论 -
安卓Kotlin 安卓6及以下 使用HttpURLConnection时 转换url路径中的汉字
URLEncoder.encode会将包括:/在内的所有字符都进行转换,因此无法直接使用,需要配合正则表达式才能达到目的。另外,安卓6以上系统使用HttpURLConnection时会针对中文等特殊字符自动转换后访问,无需再使用本函数。fun replaceChineseCharacters(string: String?) : String? { //安卓6以上自动转换,无需再用此函数 if (android.os.Build.VERSION.SDK_INT > android.os.B.原创 2021-06-30 16:38:31 · 583 阅读 · 1 评论 -
安卓Kotlin 简易带缓冲Socket 客户端(Client)
如果实现progress接口,还可以做到进度显示Client.kt本客户端在实现缓冲时还用到了安卓Kotlin 简易 ByteArray 先入先出(FIFO)队列//Fumiama 20210601//Client.ktimport android.util.Logimport java.io.*import java.lang.Thread.sleepimport java.net.Socketclass Client(private val ip: String, private.原创 2021-06-01 20:26:17 · 974 阅读 · 5 评论 -
安卓Kotlin 简易 ByteArray 先入先出(FIFO)队列
用法很简单,只需引入如下工具类,然后相应方法即可。ByteArrayQueue.kt//Fumiama 20210601//ByteArrayQueue.kt//FIFO队列class ByteArrayQueue { private var elements = byteArrayOf() val size get() = elements.size fun append(items: ByteArray) { elements += items .原创 2021-06-01 20:14:05 · 1319 阅读 · 1 评论 -
安卓Kotlin 实现控件拖动的简易工具类
使用方法方法很简单,一行代码即可。SetButtonDraggable().with(this).onto(button)工具类//fumiama 20210506import android.content.Contextimport android.view.MotionEventimport android.view.Viewimport android.widget.Buttonimport kotlin.math.absclass SetButtonDraggable {原创 2021-05-06 22:35:24 · 453 阅读 · 3 评论 -
安卓 Android JNI 添加 lzma库 并用 Kotlin调用
添加源码在官网下载源码后,将其中的C目录拷贝到src/main/cpp,并重命名为lzma添加JNI函数删除lzma/Util,并新建文件夹lzma/jni,在其下写入如下文件lzma.c。该文件提供可供JNI调用的函数lzma(sf: String, df: String, isEncode: Boolean): String,以实现压缩/解压缩文件的功能//// lzma.c// Created by rumia on 2021/3/12.///* Edit from LzmaUtil原创 2021-03-13 00:23:33 · 757 阅读 · 0 评论 -
Kotlin 计算哈希并转换为十六进制字符串
以sha-1的计算为例ByteArray转十六进制字符串函数fun toHexStr(byteArray: ByteArray){ with(StringBuilder()) { byteArray.forEach { val hex = it.toInt() and (0xFF) val hexStr = Integer.toHexString(hex) if (hexStr.length == 1) append("0").append(hexStr) else.原创 2021-02-09 20:46:24 · 1813 阅读 · 0 评论 -
安卓Kotlin 使用ContentResolver 调用系统文件管理器 读取/写入文件
读取文件(N以上无需存储权限)为兼容Android N以下设备,需要检查存储权限在AndroidManifest.xml中添加如下声明(位于application标签之外)<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>编写检查函数private fun checkReadPermission(): Boolean { return if (Build.VERSION.S原创 2020-12-18 14:03:27 · 2431 阅读 · 2 评论 -
安卓Kotlin 写入到系统剪贴板
以复制TextInputEditText中的内容为例val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManagerprivate fun copyText(t: TextInputEditText, cm: ClipboardManager){ if(t.text?.isNotEmpty() == true) { //t.selectAll() ClipData.newPlainTe.原创 2020-12-18 13:29:42 · 706 阅读 · 0 评论 -
安卓WebView 屏蔽所有类型JS弹窗
js弹窗有Alert、Prompt、Confirm三种类型,因此要分别全部加以屏蔽class MyWCC:WebChromeClient() { override fun onJsAlert( view: WebView?, url: String?, message: String?, result: JsResult? ): Boolean { result?.confirm() //模拟点击确定 .原创 2020-12-14 13:40:43 · 1408 阅读 · 0 评论 -
Gson 与 proguard 一同使用
关键要注意# Application classes that will be serialized/deserialized over Gson这一行,要写上自己的数据类。另外为了保险起见,还可以在变量前添加注解@SerializedName("varName")并在class前添加注解@Keep##---------------Begin: proguard configuration for Gson ----------# Gson uses generic type information原创 2020-09-17 11:21:25 · 388 阅读 · 0 评论 -
安卓Kotlin Glide 加载图片时使用高斯模糊
先引入如下类1:import android.content.Contextimport android.graphics.Bitmapimport android.renderscript.Allocationimport android.renderscript.Elementimport android.renderscript.RenderScriptimport android.renderscript.ScriptIntrinsicBlurimport com.bumptech.g原创 2020-09-09 00:29:01 · 683 阅读 · 0 评论 -
安卓Kotlin 解决OverscrollableNestedScrollView在fragment中使用时无法正常下拉显示appbar的问题
只需在下拉动作结束之后手动展开appbar即可package xxx.xxximport android.content.Contextimport android.util.AttributeSetimport com.akscorp.overscrollablescrollview.OverscrollableNestedScrollViewimport kotlinx.android.synthetic.main.app_bar_main.*import xxx.xxx.MainAc.原创 2020-09-08 23:32:55 · 262 阅读 · 0 评论 -
安卓Kotlin DrawerLayout 点击卡顿问题
会出现这个问题是因为点击之后未等动画结束就立即开始刷新视图,因此等结束之后再加载界面即可添加监听:var isDrawerClosed = truedrawer_layout.addDrawerListener(object : DrawerLayout.DrawerListener{ override fun onDrawerClosed(drawerView: View) { Log.d("MyMain", "onDrawerClosed") isDrawerClo.原创 2020-09-07 16:57:16 · 483 阅读 · 0 评论 -
安卓Kotlin 通过content uri 获得二进制文件并保存
private fun saveBin(uri: Uri){ val f = File(filesDir, "binFile") if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N){ uri.toFile().copyTo(f) }else { val fd = contentResolver.openFileDescriptor(uri, "r") fd?.fileDescriptor?.let { val fi = FileI原创 2020-09-05 15:07:41 · 1113 阅读 · 0 评论 -
安卓Kotlin dp转px px转dp
工具类如下import android.app.Activityimport android.app.AlertDialogimport android.util.Logimport android.widget.Toastimport java.lang.ref.WeakReferenceimport kotlin.math.sqrtclass ToolsBox(w: WeakReference<Any>) { private val zis = (w as WeakR原创 2020-08-04 16:54:19 · 1219 阅读 · 0 评论 -
安卓Kotlin 压缩多个文件为zip
如果有目录,在entry名中加入/,并且递归分析即可现在假设imgUrls数组中有一系列图片的url,我们将它下载后按index命名并进行压缩getHttpContent()函数详见 安卓Kotlin 用GET方法下载数据到ByteArrayfun packImgs(f: File):Boolean{ f.parentFile?.let { if(!it.exists()) it.mkdirs() } if(!f.exists()) f.createNewFile() va.原创 2020-08-02 22:16:40 · 779 阅读 · 0 评论 -
安卓Kotlin 获取网络类型名称
重写了get()方法,使用时直接读取该val即可val netinfo: String get() { val cm: ConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager return if (Build.VERSION.SDK_INT < 23) { cm.activeNetworkInf原创 2020-08-01 15:32:17 · 572 阅读 · 0 评论 -
安卓Kotlin 设置控件宽高比
使用该控件的OnGlobalLayoutListener即可获得其宽/高,并进行重新绘制private fun setWHRatio(v: View){ v.viewTreeObserver.addOnGlobalLayoutListener(object :ViewTreeObserver.OnGlobalLayoutListener{ override fun onGlobalLayout() { Log.d("MyRatio", "Width: $.原创 2020-08-01 12:22:34 · 923 阅读 · 0 评论 -
安卓Kotlin 实现双指缩放 双击缩放 点击翻页的ImageView
代码参考:Github PinchImageViewimport android.animation.ValueAnimatorimport android.animation.ValueAnimator.AnimatorUpdateListenerimport android.annotation.SuppressLintimport android.content.Contextimport android.graphics.Canvasimport android.graphics.Mat原创 2020-07-30 19:27:09 · 1004 阅读 · 0 评论 -
安卓Kotlin 用GET方法下载数据到ByteArray
运行下面的函数即可,传入url,返回String@ExperimentalStdlibApifun getHttpContent(Url:String):String?{ Log.d("Mydl", "getHttp: $Url") var ret:String? = null val task = FutureTask<String?>(Callable<String?> { try { val connectio原创 2020-07-30 19:11:47 · 419 阅读 · 0 评论 -
安卓Kotlin 检测ScrollView是否到头
方法很简单,只需要将布局文件中的ScrollView换成下面文件的LazyScrollView<xxx.xxx.xxx.LazyScrollView android:id="@+id/s" android:xxxx .... ....> <LinearLayout android:id="@+id/l" android:layout_width="match_parent" android:layou原创 2020-07-30 19:02:41 · 301 阅读 · 0 评论 -
安卓Kotlin 简易的properties读写工具
因为properties读写还涉及新建文件的判断以及写入项后的保存等一系列步骤,因此将其封装以便使用用法很简单,直接传入properties所在文件(不需要自己创建该文件,只需要确保程序有文件操作权限),像array那样使用即可。示例PropertiesTools(File("$filesDir/settings.properties"))["hello"]="world" //将hello项赋值为worldLog.d("MyTest", "Get hello: ${PropertiesTool.原创 2020-07-24 12:27:18 · 956 阅读 · 0 评论 -
安卓Kotlin 使用ViewPager2实现简易左右滑动翻页效果
布局很简单,R.layout.onepage仅有一个占满全屏幕的ImageView名为onei,R.layout.activity_view仅有一个占满全屏幕的ViewPager2名为vp以下为Activity的全部代码:class ViewActivity:Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) set原创 2020-07-21 23:11:20 · 2677 阅读 · 0 评论 -
安卓 设置状态栏文字亮/暗色
在styles.xml中添加项<item name="android:windowLightStatusBar">true</item>即可。值得注意的是,仅api23及以上支持该项。原创 2020-07-20 22:48:56 · 334 阅读 · 0 评论 -
安卓Kotlin 实现动态逐行逐个添加控件
思路为在竖向LinearLayout上添加多个横向LinearLayout,从而实现动态排列。控件以按钮为例竖向LinearLayout<ScrollView android:id="@+id/s" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="parent" app:lay.原创 2020-07-19 15:37:42 · 1666 阅读 · 0 评论 -
安卓Kotlin 使用AlertDlalog显示文本消息
val info = AlertDialog.Builder(this) info.setIcon(R.drawable.dnn_rnd) info.setTitle("标题") info.setMessage("正文") info.setPositiveButton(getString(R.string.ok)) {_, _->} info.setNegativeButton(getString(R.string.visit)) {_, _->}原创 2020-07-18 20:57:45 · 464 阅读 · 0 评论 -
安卓Kotlin AndroidStudio 使用Glide在ImageView加载GIF图片
在build.gradle添加apply plugin: 'kotlin-kapt'repositories { mavenCentral() maven { url 'https://maven.google.com' }}dependencies { compile 'com.github.bumptech.glide:glide:4.11.0' kapt 'com.github.bumptech.glide:compiler:4.11.0'}在代码中使用.原创 2020-07-18 20:33:10 · 912 阅读 · 0 评论 -
安卓Kotlin 启动新的Activity
在AndroidManifest.xml中添加<activity android:name=".ViewMangaActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter></activity>在上一个Activity中调用startActivity(Intent(t.原创 2020-07-17 23:06:02 · 1082 阅读 · 0 评论 -
安卓Kotlin 用GET方法保存文件到filesDir
HttpURLConnection需要新建Thread以运行fun downloadUsingUrl(Url:String?) { Thread(Runnable { try { val connection = URL(Url).openConnection() as HttpURLConnection connection.requestMethod = "GET.原创 2020-07-17 21:46:30 · 858 阅读 · 0 评论 -
安卓Kotlin API17以上 js调用Java的方法
Android4.2以前WebView 提供了javascript 调用Java代码的方法,会造成很大的安全漏洞。Android官方在API17后,限制javascript代码只能调用声明了@JavascriptInterface 注解的Java方法。所以必须要给每个可供js代码调用的Java方法加一个@JavascriptInterface接口。1//js调用代码w.settings.javaScriptEnabled = truew.addJavascriptInterface(GetH.原创 2020-07-16 21:00:51 · 253 阅读 · 0 评论 -
安卓Kotlin 用NanoHTTPD实现读取assets的内容架设http服务器
assets需要是内部类才可访问在MainActivity中继承NanoHTTPD:inner class HttpServer(port: Int) : NanoHTTPD(port) { override fun handle(session: IHTTPSession): Response { try { val file = assets.open(session.uri.toString().substring(1)).buf.原创 2020-07-02 20:10:28 · 965 阅读 · 0 评论 -
安卓kotlin利用uri保存图片到Pictures
安卓 kotlin uri 保存图片 Pictures原创 2020-03-29 18:58:33 · 1969 阅读 · 0 评论 -
安卓kotlin实现解压zip并保持目录树结构的简单方法
安卓 kotlin 解压zip 保持目录树结构 简单 copyto assets zip原创 2020-03-27 20:32:39 · 1614 阅读 · 1 评论 -
安卓 隐藏ActionBar
修改styles.xml的parent为Theme.AppCompat.NoActionBar(暗色主题)或者Theme.AppCompat.Light.NoActionBar(亮色主题)1<?xml version="1.0" encoding="utf-8"?><resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Cust原创 2020-07-20 22:40:50 · 272 阅读 · 0 评论