(入门篇)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
总结
煎和熬都是变美味的方法,加油也是!