Android video animation


Hi sir, now i am building my application to play videos from external storage. I am looking for a way to animate the video when it first loads. Please help...

By dany2012 asked on 2015-07-14
answer to this question

yuk.dara
By yuk.dara

TextureView allows you play a video. it is easy to apply an animation to the video using TextureView. You follow the steps below to create an example app on animating the video using TextureView.
1. In activity_main.xml file, you add a TextureView widget.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity"
>
<TextureView
android:id="@+id/txtview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</TextureView>
</LinearLayout

2. In the res/animator directory, you create flip.xml file. It defines a flip animation that will be applied to the TextureView.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="1000"
android:propertyName="rotationY"
android:valueFrom="0"
android:valueTo="360" >
</objectAnimator>
<objectAnimator
android:duration="1000"
android:propertyName="rotationX"
android:valueFrom="180"
android:valueTo="0" >
</objectAnimator>
</set>

3. In the MainActivity class, you write code as shown below. In this example, the video to play is The Amazing Race Watermelon Launch.3gp in external storage.
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.Surface;
import android.view.TextureView;
import java.io.IOException;
public class MainActivity extends ActionBarActivity implements TextureView.SurfaceTextureListener{
private MediaPlayer mediaPlayer;
private TextureView textureView;
AnimatorSet set;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView=(TextureView)findViewById(R.id.txtview);
textureView.setSurfaceTextureListener(this);
set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.flip);
set.setTarget(textureView);
set.start();
}
public void onSurfaceTextureSizeChanged(SurfaceTexture surface,int width, int height){
}
public void onSurfaceTextureAvailable(SurfaceTexture surface,int width, int height){
Surface surface1=new Surface(surface);
mediaPlayer=new MediaPlayer();
mediaPlayer.setSurface(surface1);
try { mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/The Amazing Race Watermelon Launch.3gp");
mediaPlayer.prepare();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.start();

} catch (IOException e) {
e.printStackTrace();
}
}
public void onSurfaceTextureUpdated(SurfaceTexture surfacet){

}
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfacet){
mediaPlayer.stop();
mediaPlayer.release();
return true;
}
}


2015-07-14 Reply





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.