安卓-常见动画的使用:帧动画、PAG动画、MP4动画

一、帧动画

在 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下面:
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

竞技而宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值