ActivityOptions.makeSceneTransitionAnimation无法在具有多个视图的kotlin中工作

时间:2018-02-24 12:48:49

标签: android animation view kotlin transition

这是我用于活动过渡动画的Kotlin代码

    val p1 = Pair.create(imageViewhospitals, "hospitals")
    val p2 = Pair.create(textViewhospitals, "title")

    val options = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        ActivityOptions.makeSceneTransitionAnimation(this, p1, p2)
                    } else {
                        TODO("VERSION.SDK_INT < LOLLIPOP")
                    }
startActivity(Intent(this,SplashActivity::class.java),options.toBundle())

我收到错误/警告,如

  

使用参数不能调用以下函数   供给

on ActivityOptions.makeSceneTransitionAnimation

5 个答案:

答案 0 :(得分:3)

val imgAnim = Pair.create<View?, String?>(galleryImg, "targetImage")
val textAnim = Pair.create<View?, String?>(textTitle, "targettext")
val options = ActivityOptionsCompat.makeSceneTransitionAnimation( activity, imgAnim, textAnim)

这对我来说使用ActivityOptionsCompat而不是使用ActivityOptions

答案 1 :(得分:2)

导入此:

[{'attribName': 'priority', 'attribDesc': 'priority of a requirement', 'attribValue': ' high,low,medium'}, {'attribName': 'prerequisite', 'attribDesc': 'User Defined Attribute', 'attribValue': ''}]

然后做

import android.util.Pair as UtilPair

答案 2 :(得分:1)

即使我使用的是Kotlin,我也必须为运行的代码指定冗余的Pair类型!所以只需添加以下内容: <View, String>

最终:

    val options = ActivityOptions.makeSceneTransitionAnimation(this,
        Pair.create<View, String>(viewHolder.imageView, getString(R.string.shared_animation_imageview)),
        Pair.create<View, String>(viewHolder.tvTitle, getString(R.string.shared_animation_title)))

    startActivity(intent, options.toBundle())

答案 3 :(得分:0)

导入

import androidx.core.util.Pair

答案 4 :(得分:0)

我遇到了同样的问题,就我而言,我需要纠正进口问题。除了导入外,这是对我有用的代码。

import androidx.core.util.Pair
import androidx.core.app.ActivityOptionsCompat
import androidx.core.app.ActivityCompat


val intent = Intent(activity, SecondActivity::class.java)
val pair1 = Pair.create<View, String>(view1, "string1")
val pair2 = Pair.create<View, String>(view2, "string2")
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pair1, pair2)
ActivityCompat.startActivity(activity, intent, options.toBundle())