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