1、创建c++工程
通过 file -> new -> new project,创建一个新的c++工程。
2、配置ndk路径
在 local.properties 文件中配置自己的ndk路径
sdk.dir=D\:\\ProgramFiles\\sdk
ndk.dir=D\:\\ProgramFiles\\sdk\\ndk
3、下载giflib源码,并拷贝到工程中
(1)giflib在源码的 external/giflib 目录下:giflib
(2)将源码文件拷贝到 main/cpp/giflib/ 目录下:
4、修改 CMakeLists.txt
拷贝完源码后,会提示头文件错误,修改CMakeLists.txt配置,执行 Build -> Refresh Linked C++ Project 。
cmake_minimum_required(VERSION 3.18.1)
project("gifdemo")
file(GLOB_RECURSE GIF_LIB ${CMAKE_SOURCE_DIR}/giflib/*.*)
add_library(
gifdemo
SHARED
${GIF_LIB} # 添加giflib
native-lib.cpp)
find_library(
log-lib
log)
target_link_libraries(
gifdemo
jnigraphics # 增加依赖
${log-lib})
5、定义 GifHandler 处理gif
public class GifHandler {
static {
System.loadLibrary("gifdemo");
}
private long gifFileInfo;
private GifHandler(long gifFileInfo) {
this.gifFileInfo = gifFileInfo;
}
// 生成GifHandler
public static GifHandler load(String path) {
long gifFileInfo = openGifFile(path);
return new GifHandler(gifFileInfo);
}
// 获取宽
public int getWidth() {
return getWidth(gifFileInfo);
}
// 获取高
public int getHeight() {
return getHeight(gifFileInfo);
}
// 将gif当前帧绘制到bitmap上
public int updateFrame(Bitmap bitmap) {
return updateFrame(gifFileInfo, bitmap);
}
/**
* 打开gif文件
*
* @param path gif路径
* @return native层GifFileType的指针
*/
public static native long openGifFile(String path);
/**
* @param gifFileInfo native层GifFileType的指针
* @return gif文件的高
*/
public static native int getHeight(long gifFileInfo);
/**
* @param gifFileInfo native层GifFileType的指针
* @return gif文件的宽
*/
public static native int getWidth(long gifFileInfo);
/**
* @param gifFileInfo native层GifFileType的指针
* @param bitmap 用于显示gif的bitmap
* @return gif当前帧的展示时长
*/
public static native int updateFrame(long gifFileInfo, Bitmap bitmap);
}
6、编写 native 代码
#include <jni.h>
#include <string>
#include <malloc.h>
#include <android/bitmap.h>
#include <string.h>
// 引入gif库
extern "C" {
#include "giflib/gif_lib.h"
}
// 存储gif的播放进度
struct GifPlayInfo {
int current_frame;
int total_frame;
};
extern "C"
JNIEXPORT jlong JNICALL
Java_com_example_gifdemo_GifHandler_openGifFile(JNIEnv *env, jclass clazz, jstring jpath) {
const char *path = env->GetStringUTFChars(jpath, 0); // 将java字符串转换成c字符串
int error;// 打开是成功还是失败;0成功,1失败
GifFileType *gifFileType = DGifOpenFileName(path, &error);
DGifSlurp(gifFileType);// 将gif读入内核
// 创建GifPlayInfo对象
GifPlayInfo *gifPlayInfo = static_cast<GifPlayInfo *>(malloc(sizeof(GifPlayInfo)));
memset(gifPlayInfo, 0, sizeof(GifPlayInfo)); // 清空
// 设置总帧数和当前播放帧
gifPlayInfo->total_frame = gifFileType->ImageCount;
gifPlayInfo->current_frame = 0;
gifFileType->UserData = gifPlayInfo; // 类似于view的setTag
// 释放字符串
env->ReleaseStringUTFChars(jpath, path);
return reinterpret_cast<jlong>(gifFileType); // 将指针返回
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_gifdemo_GifHandler_getHeight(JNIEnv *env, jclass clazz, jlong gif_file_info) {
GifFileType *gifFileType = reinterpret_cast<GifFileType *>(gif_file_info);
return gifFileType->SHeight;
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_gifdemo_GifHandler_getWidth(JNIEnv *env, jclass clazz, jlong gif_file_info) {
GifFileType *gifFileType = reinterpret_cast<GifFileType *>(gif_file_info);
return gifFileType->SWidth;
}
// 将argb转换成abgr
int32_t argb2Abgr(int alpha, GifByteType red, GifByteType green, GifByteType blue) {
return ((alpha & 0xff) << 24) | ((blue & 0xff) << 16) | ((green) << 8) | (red);
}
// 计算展示时长
int delay(ExtensionBlock *ext) {
return (10 * ((ext)->Bytes[2] << 8 | (ext)->Bytes[1]));
}
// 绘制当前帧
int myDrawFrame(GifFileType *gif, AndroidBitmapInfo *info, int *pixels, int frame_no) {
SavedImage *savedImage = &(gif->SavedImages[frame_no]); // 获取当前帧
GifImageDesc *frameInfo = &(savedImage->ImageDesc); // 当前帧的描述文件
ColorMapObject *colorMap; // 当前帧的颜色列表
if (frameInfo->ColorMap) {
colorMap = frameInfo->ColorMap;
} else {
//没有的话就获取全局的颜色列表
colorMap = gif->SColorMap;
}
ExtensionBlock *ext;
//遍历当前帧的扩展块,找到具有GRAPHICS_EXT_FUNC_CODE标志位的,这个扩展快存放着对该帧图片的处置方法,是不处理还是其他
for (int i = 0; i < savedImage->ExtensionBlockCount; ++i) {
if (savedImage->ExtensionBlocks[i].Function == GRAPHICS_EXT_FUNC_CODE) {
ext = &(savedImage->ExtensionBlocks[i]);
break;
}
}
int loc; // 当前位置
GifColorType *color; // 当前像素的颜色
int *px = pixels; // 二维指针
int *line; // 行
px = (int *) ((char *) px + info->stride * frameInfo->Top); // 计算像素,对每一个像素进行颜色填充
for (int y = frameInfo->Top; y < frameInfo->Top + frameInfo->Height; ++y) {
line = px; // 更新行数
for (int x = frameInfo->Left; x < frameInfo->Left + frameInfo->Width; ++x) {
loc = (y - frameInfo->Top) * frameInfo->Width + (x - frameInfo->Left);
//判断处置方法,拿到当前帧loc位置的字节,看是否等于扩展块中索引为3的字节,并且数值为1
if (savedImage->RasterBits[loc] == ext->Bytes[3] && ext->Bytes[0]) {
continue;
}
color = &colorMap->Colors[savedImage->RasterBits[loc]];
line[x] = argb2Abgr(255, color->Red, color->Green, color->Blue);
}
px = (int *) ((char *) px + info->stride);
}
return delay(ext);
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_gifdemo_GifHandler_updateFrame(JNIEnv *env, jclass clazz, jlong gif_file_info,
jobject bitmap) {
// 获取到gif数据
GifFileType *gifFileType = reinterpret_cast<GifFileType *>(gif_file_info);
GifPlayInfo *gifPlayInfo = static_cast<GifPlayInfo *>(gifFileType->UserData);
AndroidBitmapInfo info;// 获取bitmap信息
AndroidBitmap_getInfo(env, bitmap, &info);
void *srcBuf;// 解析bitmap,并将像素信息保存到srcBuf中
AndroidBitmap_lockPixels(env, bitmap, &srcBuf);
int delay = myDrawFrame(gifFileType, &info, (int *) srcBuf,
gifPlayInfo->current_frame); // 绘制gif
AndroidBitmap_unlockPixels(env, bitmap); // 释放锁定
gifPlayInfo->current_frame++;// 更新播放进度
if (gifPlayInfo->current_frame >= gifPlayInfo->total_frame - 1) {
gifPlayInfo->current_frame = 0;
}
return delay;
}
7、MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ActivityMainBinding binding;
private GifHandler mGifHandler;
private Bitmap mBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// 申请权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
initData();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
1);
}
}
private void initData() {
// /storage/emulated/0/Android/data/com.example.gifdemo/cache/test.gif
String path = getExternalCacheDir().getPath() + "/test.gif";
Log.v("AAAAA", path);
if (!new File(path).exists()) {
Toast.makeText(this, "gif路径无效,请重试", Toast.LENGTH_SHORT).show();
return;
}
mGifHandler = GifHandler.load(path);
mBitmap = Bitmap.createBitmap(mGifHandler.getWidth(), mGifHandler.getHeight(), Bitmap.Config.ARGB_8888);
binding.sampleText.setOnClickListener(this);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initData();
} else {
Toast.makeText(this, "请授予读写权限", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.sample_text) {
int delay = mGifHandler.updateFrame(mBitmap);
binding.iv.setImageBitmap(mBitmap);
mHandler.removeMessages(1);
mHandler.sendEmptyMessageDelayed(1, delay);
}
}
private final Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
int delay = mGifHandler.updateFrame(mBitmap);
binding.iv.setImageBitmap(mBitmap);
mHandler.removeMessages(1);
mHandler.sendEmptyMessageDelayed(1, delay);
}
}
};
}