arrayListOf(), sortedWith, compareBy Java equivalent

huangapple go评论81阅读模式
英文:

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 &lt; 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(&quot;Couldn&#39;t find supported resolution&quot;)
}

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&lt;Size&gt; resolutions = new ArrayList&lt;&gt;();
        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&lt;&gt;(Arrays.asList(...)) )

sortedWith(compareBy(...)) --> sort([Comparator or lambda expression])

huangapple
  • 本文由 发表于 2020年10月23日 07:31:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64491905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定