gpt4 book ai didi

java - arrayListOf(),sortedWith,compareBy与Java等效

转载 作者:行者123 更新时间:2023-12-02 13:06:46 24 4
gpt4 key购买 nike

我正在将基于Kotlin的Android应用程序转换为基于Java的应用程序。但是,有几种Kotlin方法(尤其是arrayListOf(), sortedWith(), compareBy()方法)存在问题。 Android Studio的转换工具带来了更加令人困惑的结果。
Kotlin中的方法:

fun getSupportedVideoSize(mediaCodec: MediaCodec, mime: String, preferredResolution: Size): Size {

if (mediaCodec.codecInfo.getCapabilitiesForType(mime)
.videoCapabilities.isSizeSupported(preferredResolution.width, preferredResolution.height))
return preferredResolution

val resolutions = arrayListOf(
Size(176, 144),
Size(320, 240),
Size(320, 180),
Size(640, 360),
Size(720, 480),
Size(1280, 720),
Size(1920, 1080)
)

val pix = preferredResolution.width * preferredResolution.height
val preferredAspect = preferredResolution.width.toFloat() / preferredResolution.height.toFloat()

val nearestToFurthest = resolutions.sortedWith(compareBy(
{
pix - it.width * it.height
},
{
val aspect = if (it.width < it.height) it.width.toFloat() / it.height.toFloat()
else it.height.toFloat()/it.width.toFloat()
(preferredAspect - aspect).absoluteValue
}))

for (size in nearestToFurthest) {
if (mediaCodec.codecInfo.getCapabilitiesForType(mime).videoCapabilities.isSizeSupported(size.width, size.height))
return size
}

throw RuntimeException("Couldn't find supported resolution")
}
Java中的转换方法:
public static Size getSupportedVideoSize(MediaCodec mediaCodec, String mime, Size preferredResolution) {
if (mediaCodec.getCodecInfo().getCapabilitiesForType(mime).getVideoCapabilities().isSizeSupported(preferredResolution.getWidth(), preferredResolution.getHeight())) {
return preferredResolution;
} else {
ArrayList<Size> resolutions = new ArrayList<>();
final int pix = preferredResolution.getWidth() * preferredResolution.getHeight();
final float preferredAspect = (float)preferredResolution.getWidth() / (float)preferredResolution.getHeight();
}

for (nearestToFurthest: Size size) {
if (mediaCodec.getCodecInfo().getCapabilitiesForType(mime).getVideoCapabilities().isSizeSupported(size.width, size.height)) {
return size;
}
}
}

最佳答案

我不了解Kotlin,但我认为这些是等效的:arrayListOf-> List.of(这是无法修改的列表,如果您确实需要arraylist,请使用new ArrayList<>(Arrays.asList(...)))sortedWith(compareBy(...))-> sort([Comparator or lambda expression])

关于java - arrayListOf(),sortedWith,compareBy与Java等效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64491905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com