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 ...
随机推荐
- CSS入门(背景各种属性的详解、垂直居中和过渡效果的详解、渐变效果的简单讲解、雪碧图和精灵图)
一.各种背景属性 1.background-image 属性为元素设置背景图像. 元素的背景占据了元素的全部尺寸,包括内边距和边框,但不包括外边距. 默认地,背景图像位于元素的左上角,并在水平和垂直方 ...
- SQL Server 2012 官方版 / SQL Server 2012下载
SQL Server是微软的一款专业免费的关系数据库管理工具, 是一个全面的数据库平台,使用集成的商业智能 (BI)工具提供了企业级的数据管理服务,SQL Server 数据库引擎为关系型数据和结构化 ...
- 东芝开发板驱动OLED模块显示LOGO图片
前言 在之前的两篇评测文章: 使用系统定时器SysTick实现精确延时微秒和毫秒函数 东芝MCU实现位带操作 介绍了系统SysTick实现精确延时,GPIO的输入输出使用,并实现了位带方式操作GPIO ...
- EntityFramework中实体类到表名的批量映射
在使用EntityFramework做CodeFirst开发时,经常需要将实体类映射到数据库表,但是C#实体类和数据库表中的命名遵循的是不同的规范,这就需要为每个实体类做一个到数据库表名的映射.大多情 ...
- default(T);
在泛型类型中,由于泛型类型即可以是引用类型也可以是值类型,所以不能用null来表示默认值.这里通过default来进行.引用类型的default将泛型类型初始化null,值类型的default将泛型类 ...
- Pyhton中变量和数据类型
一.变量 Python中变量的命名规则: 1.变量名只能包含数字.字母.下划线,且不能用数字打头. eg: message_1是对的但1_message就是错误的 2.变量名不能包含空格. 3.在变量 ...
- FCC---CSS Flexbox: Apply the flex-direction Property to Create Rows in the Tweet Embed
The header and footer in the tweet embed example have child items that could be arranged as rows usi ...
- 解决:target overrides the `GCC_PREPROCESSOR_DEFINITIONS`
[!] Please close any current Xcode sessions and use `******.xcworkspace` for this project from now o ...
- 入职小白随笔之Android四大组件——内容提供器详解(Content Provider)
Content Provider 内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间 实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的 ...
- Oracle11G_R2中共享服务器模式和专用服务器模式参数解释及设置
sys@MYTESTDB> show parameterNAME TYPE VALUE------------------------------------ ----------- ----- ...