zoukankan      html  css  js  c++  java
  • 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,操作更方便哦

  • 相关阅读:
    构建之法阅读笔记07
    7-第一阶段SCRUM冲刺
    第一阶段个人冲刺博客第十天
    第一阶段个人冲刺博客第九天
    第九周学习进度博客
    java项目(学习和研究)
    让计算机干活
    os基础
    树和图的一些算法
    java代码理解
  • 原文地址:https://www.cnblogs.com/xujunjia/p/11516937.html
Copyright © 2011-2022 走看看