首先获取一个安卓权限

<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,操作更方便哦

Android ndk 加载简单的gif 图像的更多相关文章

  1. 【转载】cocos2dx 中 Android NDK 加载动态库的问题

     原文地址:http://blog.csdn.net/sozell/article/details/10551309 cocos2dx 中 Android NDK 加载动态库的问题 闲聊 最近在接入各 ...

  2. Android 懒加载简单介绍

    1.懒加载介绍 1.1.效果预览 1.2.效果讲解 当页面可见的时候,才加载当前页面. 没有打开的页面,就不会预加载. 说白了,懒加载就是可见的时候才去请求数据. 1.3.懒加载文章传送门 参考文章: ...

  3. Android NDK加载SD卡中的so

    最近公司框架刚移植完成,由于框架程序要调用子程序,每个子程序都是一个so文件,有好几百个,把所有的so和apk打包不现实,及时可以升级维护也很麻烦.所以需要放SD卡中.考虑两种方式 1 放到设备中的 ...

  4. fackbook的Fresco (FaceBook推出的Android图片加载库-Fresco)

    [Android开发经验]FaceBook推出的Android图片加载库-Fresco   欢迎关注ndroid-tech-frontier开源项目,定期翻译国外Android优质的技术.开源库.软件 ...

  5. FaceBook推出的Android图片加载库-Fresco

    FaceBook推出的Android图片加载库-Fresco 原文链接:Introducing Fresco: A new image library for Android 译者 : ZhaoKai ...

  6. Android图片加载库:最全面的Picasso讲解

    前言 上文已经对当今 Android主流的图片加载库 进行了全面介绍 & 对比 如果你还没阅读,我建议你先移步这里阅读 今天我们来学习其中一个Android主流的图片加载库的使用 - Pica ...

  7. 演化理解 Android 异步加载图片

    原文:http://www.cnblogs.com/ghj1976/archive/2011/05/06/2038738.html#3018499 在学习"Android异步加载图像小结&q ...

  8. Android 图片加载库Glide 实战(二),占位符,缓存,转换自签名高级实战

    http://blog.csdn.net/sk719887916/article/details/40073747 请尊重原创 : skay <Android 图片加载库Glide 实战(一), ...

  9. Android图片加载框架Picasso最全使用教程1

    Picasso介绍 Picasso是Square公司开源的一个Android图形缓存库 A powerful image downloading and caching library for And ...

随机推荐

  1. angular8 导出excel文件

    angular package 1.xlsx npm install xlsx --save 2.file-saver npm install file-saver --save npm instal ...

  2. Python—解析HTML页面(HTMLParser)

    HTMLParser类的定义及常用方法 类的定义 HTMLParser主要是用来解析HTML文件(包括HTML中无效的标记). 参数convert_charrefs表示是否将所有的字符引用自动转化为U ...

  3. java面向对象 - 匿名对象

    一.匿名对象 1. 创建的对象,没有显示的赋给一个变量名,即为匿名对象. 2. 匿名对象只能调用一次 二.匿名对象使用 class Phone { private int price; public ...

  4. 面试连环炮系列(十二):说说Atomiclnteger的使用场景

    说说Atomiclnteger的使用场景 AtomicInteger提供原子操作来进行Integer的使用,适合并发情况下的使用,比如两个线程对同一个整数累加. 为什么Atomiclnteger是线程 ...

  5. .net上传文件,大文件及下载方式汇总(转)

    原文地址:http://www.360doc.com/content/19/1219/10/67993814_880731215.shtml Brettle.Web.NeatUpload.dll 文件 ...

  6. Docker Compose安装Registry后配置WebUI与客户端

    场景 Docker 私服Registry简介与使用Docker-Compose安装Registry: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...

  7. Android 时间对话框 TimePickerDialog

    private int hourOfDay, minute; @Override protected void onCreate(Bundle savedInstanceState) { super. ...

  8. mysql5.7 找回密码

    初次接触数据库,二进制安装了mysql5.7以后发现无法像yum一样在日志中找回初始密码~so 首先关掉启动的数据库: 在my.cnf中新增一句: skip-grant-tables  保存退出重启m ...

  9. 因果推理的春天系列序 - 数据挖掘中的Confounding, Collidar, Mediation Bias

    序章嘛咱多唠两句.花了大半个月才反反复复,断断续续读完了图灵奖得主Judea Pearl的The Book of WHY,感觉先读第四章的案例会更容易理解前三章相对抽象的内容.工作中对于归因问题迫切的 ...

  10. Ribbon负载均衡及Feign消费者调用服务

    微服务调用Ribbon 简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲. 这里的话 就要用到Ribbon,结合eureka,来实现服务的调用: Ribbon是Netf ...