英文:
arrayListOf(), sortedWith, compareBy Java equivalent
问题
以下是翻译好的内容:
我正在将基于 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 (Size size : nearestToFurthest) {
if (mediaCodec.getCodecInfo().getCapabilitiesForType(mime).getVideoCapabilities().isSizeSupported(size.getWidth(), size.getHeight())) {
return size;
}
}
}
英文:
I'm converting a Kotlin based Android app into a Java based one. But got a problem with several Kotlin's methods, especially with the arrayListOf(), sortedWith(), compareBy()
methods. The converting tools from Android Studio gave even more confusing result.
The method in 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")
}
Converted method in 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;
}
}
}
答案1
得分: 4
我不懂 Kotlin,但我认为下面这些是等价的:
arrayListOf
--> List.of
(这是不可修改的列表,如果你真的想要 ArrayList,请使用 new ArrayList<>()
(Arrays.asList(...)))
sortedWith(compareBy(...))
--> sort([比较器或 lambda 表达式])
英文:
I don't know Kotlin but I assume these would be equivalent:
arrayListOf
--> List.of
(this is unmodiafiable list, if you really want arraylist, use new ArrayList<>(Arrays.asList(...))
)
sortedWith(compareBy(...))
--> sort([Comparator or lambda expression])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论