一、帧动画
在 Android 中使用帧动画,可以按照以下步骤进行:
/*
代码使用:帧动画直接作为一个属性,控制属性的播放即可
*/
- 创建动画资源:将一系列帧图片保存在特定的文件夹中。
- 在布局文件中添加帧动画视图:使用 ImageView 或其他支持帧动画的视图。
- 设置动画资源:通过属性或代码设置帧动画的资源。
- 控制动画播放:可以通过代码控制动画的播放、停止、循环等。
1、创建动画资源:将一系列帧图片保存在特定的文件夹中
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/f_00025" android:duration="120"/>
<item android:drawable="@drawable/f_00026" android:duration="120"/>
<item android:drawable="@drawable/f_00027" android:duration="120"/>
<item android:drawable="@drawable/f_00028" android:duration="120"/>
<item android:drawable="@drawable/f_00029" android:duration="120"/>
<item android:drawable="@drawable/f_00030" android:duration="120"/>
<item android:drawable="@drawable/f_00031" android:duration="120"/>
<item android:drawable="@drawable/f_00032" android:duration="120"/>
</animation-list>
2、在布局文件中添加帧动画视图:使用 ImageView 或其他支持帧动画的视图。
<LinearLayout
android:id="@+id/ll_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_centerHorizontal="true"
android:background="@drawable/frame"
android:orientation="horizontal" />
3、设置动画资源:通过属性或代码设置帧动画的资源。
mFrameLL = findViewById(R.id.ll_frame);
AnimationDrawable mFameAnimation = (AnimationDrawable) mFrameLL.getBackground();
4、控制动画播放:可以通过代码控制动画的播放、停止、循环等。
mFameAnimation.start();
//..
mFameAnimation.stop();
二、PAG动画
/*
代码使用:PAG动画使用PAGView来监管pag动效的使用
*/
LinearLayout pagLL = findViewById(R.id.ll_pag);
pagView = new PAGView(this);
pagView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
pagLL.addView(pagView);//需要添加View,也就是动画
// 加载 PAG 文件
PAGComposition composition = PAGFile.Load(getAssets(), "countdown.pag");
pagView.setComposition(composition);
// 设置动效参数
pagView.setRepeatCount(1);
//添加动效监听
pagView.addListener(new PAGView.PAGViewListener() {
@Override
public void onAnimationStart(PAGView view) {
Log.i(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(PAGView view) {
Log.i(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(PAGView view) {
Log.i(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(PAGView view) {
}
@Override
public void onAnimationUpdate(PAGView pagView) {
}
});
//播放
pagView.play();
其中,pag动效通常放在assets文件夹下
三、MP4动画
在安卓中,可以使用相应的开发工具和 API 来处理 MP4 动画,例如:
MediaPlayer player = MediaPlayer.create(getApplicationContext(), Uri.parse("your_mp4_file.mp4"));
player.start();
要将 MediaPlayer 放到 TextureView 中播放,可以按照以下步骤进行:
- 创建 TextureView 对象。
- 创建 MediaPlayer 对象,并设置要播放的资源。
- 在适当的时机调用 MediaPlayer的播放方法。
- 将 MediaPlayer 输出的视频内容渲染到 TextureView 上。
TextureView textureView;
MediaPlayer player;
// 创建 TextureView 对象
textureView = findViewById(R.id.textureView);
// 创建 MediaPlayer 对象并设置资源
player = MediaPlayer.create(getApplicationContext(), Uri.parse("your_media_file"));
// 准备完成后开始播放
player.prepare();
player.start();
// 将视频内容渲染到 TextureView 上
TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
player.setSurface(new Surface(surfaceTexture));
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
player.release();
return false;
}
};
textureView.setSurfaceTextureListener(textureListener);
其中,MP4动效通常放在raw下面: