Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 555
Hossein Seifi Avatar asked Jun 17 '18 20:06

Hossein Seifi


People also ask

How do I animate a view in Android?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.

Is animation possible on Android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.

What can be used to animate between two or more views in Android?

The transitions framework can run animations between a starting and an ending scene. You can create your scenes from a layout resource file or from a group of views in your code. However, the starting scene for your transition is often determined automatically from the current UI.


1 Answers

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() {}

});
like image 72
Niza Siwale Avatar answered Oct 13 '22 16:10

Niza Siwale