First obtain a permission Andrews
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
Creating a class GifInfoHandle
And calling c ++ Interface
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 the synchronized int getHeight () { return getHeight (gifInfoPtr); } // call Native Private Native Long the openFile (String path); // open the file path Private Native Long renderFrame ( Long gifInfoPtr, Bitmap Bitmap) ; // Get the frame rate Private Native int getWidth ( Long gifInfoPtr); // Get the width Private Native int getHeight ( LonggifInfoPtr); // get the height }
To get the callback interface to configure c ++ Code
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); }
Here with a dynamic library on this site acquisition https://sourceforge.net/projects/giflib/
To get the callback method through the main page
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); } }; }
Here is the source code
Link: https: //pan.baidu.com/s/133c9Fk7BXwPrugP5wCrv5A
extraction code: 5wa1
copy the contents of this open Baidu network disk phone App, the operation more convenient oh