将乐天动画保存/导出为视频(mp4或其他视频格式):android

时间:2019-11-20 06:13:49

标签: android video lottie lottie-android

我想将抽奖动画另存为视频(mp4或任何其他视频格式)。我在Google搜索了很多东西。我发现了很多参考资料,但没有运气。让我分享我所有的研究

第一

How to save Lottie Animation as Video (.mp4) and GIF (.gif) in Android?

第二

我尝试了这个Lottie Recorder Test lib。但我仍然得到issue with this lib also

第三

有人告诉我record surfaceview using this lib。但是How to draw each frame of lottie onto the canvas?

我没有从任何地方得到解决方案。我尝试了许多演示,也尝试解决/修改,但没有运气。我真的知道知道如何从抽奖中在Surfaceview中绘制每个框架

public class MyLottieAnimationView extends LottieAnimationView {
SurfaceHolder holder;

public void setHolder(SurfaceHolder holder) {
    this.holder = holder;
}

public MyLottieAnimationView(Context context) {
    super(context);
}

public MyLottieAnimationView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyLottieAnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onDraw(Canvas canvas) {

    Canvas c = holder.lockCanvas();
    if (c == null) {
        Log.e(TAG, "Cannot draw onto the canvas as it's null");
    } else {
        Bitmap myBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.RGB_565);
        canvas.setBitmap(myBitmap);

        c.drawBitmap(myBitmap, 0, 0, new Paint());
//            c.
//            drawMyStuff(canvas);
        holder.unlockCanvasAndPost(canvas);
    }

    super.onDraw(canvas);
}

}

1 个答案:

答案 0 :(得分:1)

val lottieComposition = LottieCompositionFactory.fromRawResSync(this, R.raw.android_wave) // your lottie json file 
    val lottieDrawable = LottieDrawable()
    lottieDrawable.composition = lottieComposition.value




val path = getExternalFilesDir(Environment.DIRECTORY_PICTURES) ?: File(cacheDir, Environment.DIRECTORY_PICTURES).apply { mkdirs() }
    val videoFile = File(path, "lottie_in_video.mp4")
    val recordingOperation = RecordingOperation(Recorder(videoOutput = videoFile), FrameCreator(lottieDrawable))
    {
      textView.text = getString(R.string.recording_finished)
      openCreatedVideo(videoFile)
    }

然后调用保存按钮

recordingOperation.start()

引用:https://github.com/rogererill/LottieRecorderTest