Android ndk 加载简单的gif 图像
首先获取一个安卓权限
<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 图像的更多相关文章
- 【转载】cocos2dx 中 Android NDK 加载动态库的问题
原文地址:http://blog.csdn.net/sozell/article/details/10551309 cocos2dx 中 Android NDK 加载动态库的问题 闲聊 最近在接入各 ...
- Android 懒加载简单介绍
1.懒加载介绍 1.1.效果预览 1.2.效果讲解 当页面可见的时候,才加载当前页面. 没有打开的页面,就不会预加载. 说白了,懒加载就是可见的时候才去请求数据. 1.3.懒加载文章传送门 参考文章: ...
- Android NDK加载SD卡中的so
最近公司框架刚移植完成,由于框架程序要调用子程序,每个子程序都是一个so文件,有好几百个,把所有的so和apk打包不现实,及时可以升级维护也很麻烦.所以需要放SD卡中.考虑两种方式 1 放到设备中的 ...
- fackbook的Fresco (FaceBook推出的Android图片加载库-Fresco)
[Android开发经验]FaceBook推出的Android图片加载库-Fresco 欢迎关注ndroid-tech-frontier开源项目,定期翻译国外Android优质的技术.开源库.软件 ...
- FaceBook推出的Android图片加载库-Fresco
FaceBook推出的Android图片加载库-Fresco 原文链接:Introducing Fresco: A new image library for Android 译者 : ZhaoKai ...
- Android图片加载库:最全面的Picasso讲解
前言 上文已经对当今 Android主流的图片加载库 进行了全面介绍 & 对比 如果你还没阅读,我建议你先移步这里阅读 今天我们来学习其中一个Android主流的图片加载库的使用 - Pica ...
- 演化理解 Android 异步加载图片
原文:http://www.cnblogs.com/ghj1976/archive/2011/05/06/2038738.html#3018499 在学习"Android异步加载图像小结&q ...
- Android 图片加载库Glide 实战(二),占位符,缓存,转换自签名高级实战
http://blog.csdn.net/sk719887916/article/details/40073747 请尊重原创 : skay <Android 图片加载库Glide 实战(一), ...
- Android图片加载框架Picasso最全使用教程1
Picasso介绍 Picasso是Square公司开源的一个Android图形缓存库 A powerful image downloading and caching library for And ...
随机推荐
- java月考题JSD1908第二次月考(含答案和解析)
考试 .container { clear: both; margin: 0 auto; text-align: left; /*width: 1200px;*/ } .container:after ...
- SMTP email from C#
/// <summary> /// 一人一附件发送邮件 /// 2017-05-17 涂聚文 GeovinDu /// </summary> /// <param nam ...
- Markdown的常用方法总结
1.标题 # 大标题 ## 副标题 ### 小标题 标准 2.强调 *斜体类型* **黑体字** 3.折叠 折叠长句 <details><summary>Boostnote是对 ...
- `MediaDevices.getUserMedia` `undefined` 的问题
通过 MediaDevices.getUserMedia() 获取用户多媒体权限时,需要注意其只工作于以下三种环境: localhost 域 开启了 HTTPS 的域 使用 file:/// 协议打开 ...
- 用Python6种方法:给定一个不超过5位的正整数,判断有几位
方法一:作比较 a=int(input(">>>>")) if a<10: print(1) elif a<100: #第一个条件已经过滤了大于 ...
- Java生鲜电商平台-服务化后的互联网架构实战(针对生鲜电商小程序或者APP)
Java生鲜电商平台-服务化后的互联网架构实战(针对生鲜电商小程序或者APP) “微服务架构”的话题非常之火,很多朋友都在小窗我,说怎么做服务化?解答“怎么做”之前,先得了解“为什么做”. 画外音:做 ...
- JavaScript设计模式基础(一)
模式的起源 模式 起源于建筑学.20世纪70年代,哈佛大学建筑学博士Christopher Alexander和他的团队花大约20年,来研究为解决同一个问题而设计出的不同建筑结构,从中发现那些高质量设 ...
- [转]自定义UiPath Activity实践
本文转自:https://segmentfault.com/a/1190000017440647 为了对UiPath Activity的实现方式一探究竟,自己尝试实践编写了一个简单的Activity, ...
- 深入解析ReentrantReadWriteLock
前言: 在Java的锁中很多锁都是同一时刻只允许一个线程访问,今天就来看看一个特殊的锁——读写锁.它的特殊之处就在于同一时刻可以运行多个读线程访问或者有一个写线程在访问.能够大大的提高并发性和吞吐量 ...
- 服务守护DOS脚本
创建一个批处理文件,复制以下内容至文件中并保存,右键文件名,以管理员身份运行. @@@code @echo off @echo 请使用管理员身份运行此脚本 rem 运行前先打开文件修改下列变量: ...