首先获取一个安卓权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

创建一个GifInfoHandle  类

并且调用c++接口

package com.example.ndkdemo;

import android.graphics.Bitmap;

public class GifInfoHandle {
private volatile long gifInfoPtr;
static {
System.loadLibrary("android_gif");
}
public GifInfoHandle(String path)
{
gifInfoPtr=openFile(path);
}
public long renderFrame(Bitmap bitmap)
{
return renderFrame(gifInfoPtr,bitmap);
}
public synchronized int getwidth()
{
return getwidth(gifInfoPtr);
}
public synchronized int getHeight()
{
return getheight(gifInfoPtr);
}
//调用 native
private native long openFile(String path); //打开文件路径
private native long renderFrame( long gifInfoPtr, Bitmap bitmap); //获取帧率
private native int getwidth(long gifInfoPtr); //获取宽度
private native int getheight(long gifInfoPtr); //获取高度
}

通过配置c++代码获取回调接口

jint getwidth(GifInfo *info){

    return info->originalWidth;
}
jint getHeight(GifInfo *info){ return info->originalHeight; }
JNIEXPORT jlong JNICALL
Java_com_example_ndkdemo_GifInfoHandle_openFile(JNIEnv *env, jobject thiz, jstring path) {
// TODO: implement openFile()
return openFile(env,path);
} JNIEXPORT jlong JNICALL
Java_com_example_ndkdemo_GifInfoHandle_renderFrame(JNIEnv *env, jobject thiz, jlong gif_info_ptr,
jobject bitmap) {
// TODO: implement renderFrame() return renderFrame(env,gif_info_ptr,bitmap);
} JNIEXPORT jint JNICALL
Java_com_example_ndkdemo_GifInfoHandle_getwidth(JNIEnv *env, jobject thiz, jlong gif_info_ptr) {
// TODO: implement getwidth()
getwidth(gif_info_ptr);
} JNIEXPORT jint JNICALL
Java_com_example_ndkdemo_GifInfoHandle_getheight(JNIEnv *env, jobject thiz, jlong gif_info_ptr) {
// TODO: implement getheight()
getHeight(gif_info_ptr);
}

这里用的动态库在这个网站获取 https://sourceforge.net/projects/giflib/

通过主页面获取回调方法

package com.example.ndkdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView; import java.io.File; public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' library on application startup.
private ImageView imageView;
private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ds.gif";
private Bitmap bitmap;
private GifInfoHandle infoHandle; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("TAG", "onCreate: "+path );
imageView=findViewById(R.id.image);
infoHandle=new GifInfoHandle(path);
int width=infoHandle.getwidth();
int height=infoHandle.getHeight(); bitmap =Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
imageView.setImageBitmap(bitmap);
long nextFrameTime=infoHandle.renderFrame(bitmap);
infoHandle.renderFrame(bitmap);
//循环绘制
handler.sendEmptyMessageDelayed(1,nextFrameTime);
}
Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
long nextFrameTime =infoHandle.renderFrame(bitmap);
imageView.setImageBitmap(bitmap);
handler.sendEmptyMessageDelayed(1,nextFrameTime);
}
};
}

下面是这次的源码

链接:https://pan.baidu.com/s/133c9Fk7BXwPrugP5wCrv5A
提取码:5wa1
复制这段内容后打开百度网盘手机App,操作更方便哦

最新文章

  1. (转)客户端触发Asp.net中服务端控件事件
  2. checkbox提交多组数据到action
  3. C实现通用数据结构--双向链表
  4. 利用jdbc连接oracle数据库
  5. maven打成war包之后没有class文件
  6. s3cmd的安装与配置
  7. Linux 性能监控的18个命令行工具
  8. MySQL 约束
  9. [原]用C#模拟实现扑克牌发牌、排序程序。
  10. ASP.NET之电子商务系统开发-4(二级分类)
  11. 蓝桥杯 BASIC 27 矩阵乘法(矩阵、二维数组)
  12. js拖拽分析
  13. ROS_Kinetic_07 ROS中机器人三维物理引擎高保真仿真利器gazebo 7.0
  14. Angular TypeScript开发环境集成jQuery扩展插件
  15. 继续沿用旧的网络访问模式Apache HTTP 客户端,防止Android9闪退
  16. 第五节:WebApi的三大过滤器
  17. todo项目总结
  18. 【题解】 bzoj3555: [Ctsc2014]企鹅QQ (字符串Hash)
  19. 前端工程化系列[05] Yeoman脚手架使用入门
  20. Rabbit五种消息队列学习(一) – 总述

热门文章

  1. Vue ES6箭头函数使用总结
  2. View 的绘制过程
  3. day07什么是方法、方法的定义、方法的调用
  4. npm 镜像配置
  5. JavaScript图形实例:四瓣花型图案
  6. JS完美拖拽
  7. Mass Assignment:Request Parameters Bound into Persisted Objects 质量分配:请求绑定到持久对象中的参数
  8. php 将科学计算法得出的结果转换成原始数据 NumToStr
  9. Java 入土之路
  10. C++标准库之string类型