我有一个PreviewView占据整个屏幕但工具栏除外。相机的预览效果很好,但是当我拍摄图像时,纵横比完全不同。
我想在成功捕获后向用户显示图像,因此它的大小与 相同,PreviewView因此我不必裁剪或拉伸它。
是否可以在每个设备上更改纵横比,它是它的大小PreviewView还是我必须将其设置为固定值?
我想使用 Kotlin Flow 来处理 FirebaseAuth 状态。我知道下面的代码是错误的,但我不知道如何修复它。我试过了channelFlow,它总是在我想要的时候崩溃,send或者offer
fun registerFlow(email: String, password: String) = flow {
emit(AuthState.Loading)
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
CoroutineScope(Dispatchers.IO).launch {
emit(AuthState.Success(task.result?.user))
} }else {
emit(AuthState.Error(task.exception))
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
里面的协程Listener抛出
z E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: pl.rybson.musicquiz, PID: 26578
java.lang.IllegalStateException: Flow invariant is violated:
Emission from another coroutine is detected.
Child of StandaloneCoroutine{Active}@4903a45, expected child of StandaloneCoroutine{Completed}@988059a.
FlowCollector is not thread-safe and concurrent emissions are prohibited.
To …Run Code Online (Sandbox Code Playgroud) 我正在使用带有导航抽屉的 Android 导航组件(如在 Android Studio 模板中)。我将片段 A、B、C 作为顶级片段,用于导航抽屉中,片段 Z 与导航图中的片段 A 相连。现在我在片段 A 中有一个按钮。单击该按钮将使用安全参数打开片段 Z。
binding.button.setOnClickListener {
val action = NewsFragmentDirections.actionNavNewsToNewsDetailsFragment()
it.findNavController().navigate(action)
}
Run Code Online (Sandbox Code Playgroud)
打开片段 Z 时,应用栏图标会自动变为后退按钮,这将允许我返回片段 A。
这些工作正常,但这个问题是,当我在实时数据 obsedrver 中使用相同的安全参数代码时,后退按钮不起作用。
viewModel.actionNewsDetails.observe(viewLifecycleOwner, {
val action = NewsFragmentDirections.actionNavNewsToNewsDetailsFragment()
findNavController().navigate(action)
})
Run Code Online (Sandbox Code Playgroud)
这里有一些额外的细节
我一直在努力解决这个问题。对不起,我的英语不好。
android kotlin android-jetpack android-architecture-navigation
刚刚了解到DataBinding并发现toString()Kotlin的强大内置功能不可用:
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="student"
type="com.example.databindingtest2.Student" />
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{student.name}"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@{student.age.toString()}" //doesn't work, age is integer
android:textColor="@android:color/black"
android:textSize="30sp" />
</layout>
Run Code Online (Sandbox Code Playgroud)
我知道String.valueOf()会起作用,但这不是 Kotlin 的方式。任何帮助,将不胜感激。
android kotlin kotlin-extension android-databinding android-jetpack
如果我在项目和该项目的依赖项中有不同的 Kotlin 版本,我不知道会遇到什么问题。
选项1:
1.3.72okhttp例如)包括 Kotlin 1.4.10。(我们现在为 Authenticator、Interceptor 等使用 Kotlin 1.4.x 功能接口。)选项 2:
1.4.01.3.71在选项 2 中,我收到下一个警告:
w: 类路径中的运行时 JAR 文件应该具有相同的版本。这些文件位于类路径中: /.gradle/caches/transforms-2/files-2.1/ab41544fa06f7b55dec847efe3b9899c/jetified-kotlin-stdlib-jdk7-1.3.71.jar (version 1.3) .transforms-cache/s/文件-2.1 / a4c6c3b949eb40b555dea1468ae75f87 / jetified-科特林-STDLIB-1.4.10.jar(1.4版本).gradle /高速缓存/变换-2 /文件-2.1 / f98f62bf33e752598311020043618780 / jetified-科特林-STDLIB-共1.4.10.jar(版本 1.4)
w:类路径中的某些运行时 JAR 文件的版本不兼容。考虑从类路径中删除它们
我正在努力理解 Chocolate Doom 如何分配其余弦表。注释解释说,为了计算余弦表,它在正弦表上进行了 PI/2 移位,但我不明白它是如何使用 C 完成的。
这是 Chocolate Doom 的原始源代码tables.h和tables.c
#define FINEANGLES 8192
const fixed_t finesine[10240] =
{
25,75,125,175,226,276,326,376,
...
}
const fixed_t *finecosine = &finesine[FINEANGLES/4];
Run Code Online (Sandbox Code Playgroud)
这就是我试图在 Kotlin 中实现的目标
val fineSine: Array<Fixed_t> = arrayOf(25,75,125,175,226,276,326,376, ...)
val fineCosine: Array<Fixed_t> = arrayOf() // This is where I'm stuck
Run Code Online (Sandbox Code Playgroud) 我正在尝试从我的应用程序生成一些 QR,但我看到有很多类型的 QR,例如联系人、Wi-Fi 等。我想知道是否有免费的 API 或库来实现这一点,我已经看到有些网站已经在生成它,所以我想知道 Android 是否有任何 API 或库可以使用。
我检查过的:
但我不确定是否有一个功能可以说好的,我想要一个联系人的二维码,这样我就可以添加它的所有信息。
我正在尝试运行使用 okhttp3 进行 Rest api 调用的示例代码。
MoshiJsonConvertor.kt:
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
class MoshiJsonConvertor {
private val client = OkHttpClient()
private val moshi = Moshi.Builder().build()
private val gistJsonAdapter = moshi.adapter(Gist::class.java)
fun run() {
val request = Request.Builder()
.url("https://api.github.com/gists/c2a7c39532239ff261be")
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
val gist = gistJsonAdapter.fromJson(response.body!!.source())
for ((key, value) in gist!!.files!!) {
println(key)
println(value.content)
}
}
}
@JsonClass(generateAdapter = true)
data class Gist(var files: Map<String, GistFile>?)
@JsonClass(generateAdapter = …Run Code Online (Sandbox Code Playgroud) 安卓工作室 3.6
val reversedEventList: List<Event> = (response.body() as List<Event>).reversed()
Debug.d(TAG, "loadEvents: reversedEventList(" + reversedEventList.size + ")")
val lastEvent: Event = reversedEventList.last {
it.info?.deviceId.equals(
DeviceUtil.deviceId,
true
) && it.type == Type.CALL_WAITRESS
}
Debug.d(TAG, "loadEvents: lastEvent = $lastEvent)")
Run Code Online (Sandbox Code Playgroud)
在这里logcat:
MainViewModel( 5086): loadEvents: reversedEventList(338)
MainViewModel( 5086): loadEvents: lastEvent = Event{id = 366, orgn = 17, type = CALL_WAITRESS, started = 1566750174, finished = 1566750194, closeUser = 1, info = Info{isCash=false, isDiscount=false, deviceId = 41c2d0af61254f94a948882c7613d873}, user = '0', room = 1, table = 66, …Run Code Online (Sandbox Code Playgroud) 我有一个具有以下格式的日期字符串:MM/DD/2019
可能的值可以是1/16/2019或11/31/2019例如。
我正在寻找一种方法来转换这个字符串值以获得以下格式:
2019-11-31 15:07:57.013Z
Run Code Online (Sandbox Code Playgroud)
我怎么能在 Kotlin 中做到这一点?