Hossein Seifi
Hossein Seifi

Reputation: 1420

How to make a video file by capturing the animated view in android or java?

I have made an imageView animate from one side to the other side of the screen. Here is the java code:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView imageView = findViewById(R.id.imageView);
        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleAnimation(imageView);
            }
        });
    }
    public void handleAnimation(View view) {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "x", 1000f);
        animatorX.setDuration(2000);
        animatorX.start();
    }
}

And this is what we see when user clicks on the ANIMATE button: enter image description here

Now my question is that how I can make a video file by capturing the animated imageView ?

EDIT:

What I need is: I want to make an app which takes some photos from the user and make some animations on the photos and some effects and also mix them with a desired sound and at the end exports a video clip. And of course if I can I would rather make all these things hidden.

Upvotes: 10

Views: 4134

Answers (2)

Niza Siwale
Niza Siwale

Reputation: 2404

You have to record your screen and then crop the video using your view's xy coordinates. You can record your screen using the MediaProject API on android (5) and above.

 private VirtualDisplay mVirtualDisplay;
    private MediaRecorder mMediaRecorder;
    private MediaProjection mMediaProjection;
    private MediaProjectionCallback callback;

 MediaProjectionManager projectionManager = (MediaProjectionManager) 
        context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        mMediaProjection.registerCallback(callback, null);
        initRecorder();
        mMediaRecorder.prepare();
        mVirtualDisplay = createVirtualDisplay();
        mMediaRecorder.start();


public void initRecorder() {
        path = "/sdcard/Record/video" + ".mp4";
        recId = "capture-" + System.currentTimeMillis() + ".mp4";
        File myDirectory = new File(Environment.getExternalStorageDirectory(), "Record");

            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mMediaRecorder.setVideoEncodingBitRate(MainFragment.bitRate);          
            mMediaRecorder.setVideoFrameRate(30);
            mMediaRecorder.setVideoSize(MainFragment.DISPLAY_WIDTH,
            MainFragment.DISPLAY_HEIGHT);
            mMediaRecorder.setOutputFile(path);
    }


    private VirtualDisplay createVirtualDisplay() {
        return mMediaProjection.createVirtualDisplay("MainActivity",
                MainFragment.DISPLAY_WIDTH, MainFragment.DISPLAY_HEIGHT, MainFragment.screenDensity,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
    }

public class MediaProjectionCallback extends MediaProjection.Callback {
        @Override
        public void onStop() {
            mMediaRecorder.stop();
            // mMediaRecorder.reset();
            mMediaRecorder.release();
            mMediaProjection.unregisterCallback(callback);
            mMediaProjection = null;
            mMediaRecorder = null;
        }

Once done simply call mMediaProjection.stop() to finish the recording and save the video as tmp After which you can crop the video at the xy coordinates that your view is position using FFmpeg

ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4

Where the options are as follows:

out_w is the width of the output rectangle

out_h is the height of the output rectangle

x and y specify the top left corner of the output rectangle

so in your case

String cmd ="-i '"+ tmpVideoPath+"' -filter:v "+"'crop="+view.getWidth()+":"+view.getHeight()+":"+view.getX()+":"+view.getY()+"'"+" -c:a copy "+outVideoPath
FFmpeg ffmpeg = FFmpeg.getInstance(context);
  // to execute "ffmpeg -version" command you just need to pass "-version"
ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onProgress(String message) {}

    @Override
    public void onFailure(String message) {}

    @Override
    public void onSuccess(String message) {}

    @Override
    public void onFinish() {}

});

Upvotes: 6

Maraj Hussain
Maraj Hussain

Reputation: 1596

There are two possible approaches to archive this.

1- You can acchive this by using the javacv library (FFmpeg) to combine a set of bitmaps taken from the view

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("/sdcard/test.mp4",256,256);
try {
    recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
    recorder.setFormat("mp4");
    recorder.setFrameRate(30);
    recorder.setPixelFormat(avutil.PIX_FMT_YUV420P10);
    recorder.setVideoBitrate(1200);
    recorder.startUnsafe();
    for (int i=0;i< 5;i++)
    {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        recorder.record(bitmap);
    }
    recorder.stop();
}
catch (Exception e){
    e.printStackTrace();
}  

all the code of using this library is here

2- You can use this link for record the screen and use as per your need.
Screen Recorder

Upvotes: 0

Related Questions