android中PAG的简单使用

本文介绍了Android中PAG(Portable Animated Graphics)的简单使用,包括什么是PAG,如何引入库,以及如何读取和显示PAG图片。在使用过程中,可能会遇到的 UnsatisfiedLinkError 问题的解决办法也进行了说明,并提供了官方代码和API参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(入门篇)android中PAG的简单使用

一、pag是什么?

示例:pag官网

二、使用步骤

1.引入库

代码如下(示例):在app文件夹下的build.gradle文件中,添加下面这条依赖,添加到dependencies {}中。

implementation 'com.tencent.tav:libpag:latest.release'

如果最后运行时候会报java.lang.UnsatisfiedLinkError: No implementation found for void org.libpag.PAGPlayer.nativeInit() 这个错误的话,就在这个build.gradle这个文件下加这个

ndk {
            abiFilters 'x86','armeabi-v7a','arm64-v8a','armeabi'
        }

加入的位置是这个位置
在这里插入图片描述
然后在main文件下创建assets文件夹,将pag图片放入里面
在这里插入图片描述

2.读入数据

UI代码如下(示例):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <RelativeLayout
        android:id="@+id/background_view"
        android:layout_width="410dp"
        android:layout_height="500dp"/>

</LinearLayout>

activity中的代码如下:

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.libpag.PAGFile;
import org.libpag.PAGView;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    private TextView text;
    PAGFile pagFile1 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.text);
        
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RelativeLayout backgroundView = findViewById(R.id.background_view);
                PAGView pagView = new PAGView(MainActivity.this);
                pagView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                backgroundView.addView(pagView);
				
				//这里填入图片
                pagFile1 = PAGFile.Load(getAssets(), "0.pag");
                pagView.setComposition(pagFile1);

                pagView.setRepeatCount(0);
                pagView.play();
            }
        });
    }
}

如果感觉还是不太行的话,可以去查看官网给的代码和API
官网的代码:官网android代码
官网的API:官网androidAPI


总结

煎和熬都是变美味的方法,加油也是!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值