2

我是 Flutter 的新手,但我读到在 Flutter 中没有简单的方法可以像 Android 允许的那样为矢量绘制动画。

我拥有一个矢量进度条,我想对其进行动画处理以取得进展。然后我尝试使用平台通道获取 Android / iOS 本机代码来为我的矢量设置动画。

现在我只测试了Android,这是我的代码,由于视图而无法正常工作:

颤振:

static const progressChannel = const MethodChannel("progress");
  Widget progressBar;

  Future<void> _getProgressBar() async {
    Widget progressBarView;
    try {
      var result = await progressChannel.invokeMapMethod('start');
      print("Result : ---- " + result.toString());
      progressBarView = result as Widget;
    }
    catch (e) {
      print("error channel : " + e.toString());
    }

    setState(() {
      progressBar = progressBarView;
    });
  }

安卓 :

class MainActivity: FlutterActivity() {

    private val CHANNEL = "progress"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "start") {
                val view = ImageView(this)
                result.success(startProgressBar(view))
            }
        }
    }

    private var vector: AnimatedVectorDrawableCompat? = null

    fun startProgressBar(view: ImageView): ImageView {
        val mainHandler = Handler(Looper.getMainLooper())
        vector = AnimatedVectorDrawableCompat.create(view.context, R.drawable.avd_progress_bar)
        view.setImageDrawable(vector)
        if (vector != null) {
            vector!!.registerAnimationCallback(object : androidx.vectordrawable.graphics.drawable.Animatable2Compat.AnimationCallback() {
                override fun onAnimationEnd(drawable: Drawable) {
                    super.onAnimationEnd(drawable)
                    mainHandler.post { vector!!.start() }
                }
            })
            vector!!.start()
        }
        return view
    }
}

显然我无法ImageView从 Android 代码创建并检索它。错误 :

E/MethodChannel#progress(32414): Failed to handle method call
E/MethodChannel#progress(32414): java.lang.IllegalArgumentException: Unsupported value: android.widget.ImageView{40dfc22 V.ED..... ......ID 0,0-0,0}

有没有办法创建一个原生View并从 Flutter 中检索它?或者另一种方式来实现这一点?

4

0 回答 0