Kanzi for Android Demo

本文介绍了如何在Android应用中新建HelloKanzi工程,包括将kzb和cfg文件复制到assets目录,引入Kanzi依赖库,配置build.gradle,以及在MainActivity中设置和使用Kanzi。重点展示了如何处理data.xml配置文件和初始化KanziDataSourceManager。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新建HelloKanzi工程

拷贝kzb等文件到assets目录

/.../app/src/main/assets$ tree -l
.
├── a55testapplication.kzb
├── application.cfg
├── data.xml
├── tsview.kzb.cfg
└── tsview.kzb.txt

其中data.xml文件的接口描述文件:

assets$ cat data.xml 
<speed type = "int">1</speed>

引入Kanzi依赖库

拷贝文件:

app/src/main/libs$ tree -l
.
├── AndroidDataSource.jar
├── kanzi_engine.jar
└── arm64-v8a
    ├── libc++_shared.so
    ├── libkanzi.so
    ├── libkzandroiddatasource.so
    ├── libkzappmanager.so
    ├── libkzcore.so
    ├── libkzcoreui.so
    ├── libkzparticlesystem.so
    └── libkzui.so

在build.gradle中引入依赖:

android {
    defaultConfig {
        ...
        ndk {
            abiFilters 'arm64-v8a'    //只生成armv8的so
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    aaptOptions {
        noCompress "cfg","kzb"
    }
}

dependencies {
    ...
    implementation files('libs/AndroidDataSource.jar')
    implementation files('libs/kanzi_engine.jar')
    ...
}

修改MainAcitivity代码

package com.xxx.hellokanzi;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.rightware.kanzi.KanziActivity;
import com.rightware.kanzi.androiddatasource.AndroidDataSourceManager;
import com.rightware.kanzi.androiddatasource.BaseManager;
import com.rightware.kanzi.androiddatasource.SharedData;
import com.rightware.kanzi.androiddatasource.kzDataTypeInt;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends KanziActivity {

    private static final String TAG = "KanziActivity";

    AndroidDataSourceManager mDataFeeder;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        String kanziConfigPath = getFilesDir().getAbsolutePath();

        copyAssetsToDst(this, "data.xml", kanziConfigPath + File.separator + "data.xml");

        mDataFeeder = new AndroidDataSourceManager("AndroidDataSourceManager");
        SharedData.get().addManager((BaseManager) mDataFeeder);

        try {
            mDataFeeder.setXMLPath(kanziConfigPath + File.separator);
        } catch (Exception exception) {
            Log.e(TAG, exception.toString());
        }


        queueEvent(new Runnable() {
            @Override
            public void run() {
                if (mDataFeeder == null) {
                    return;
                }
                kzDataTypeInt intDataType = new kzDataTypeInt("speed");
                mDataFeeder.getKanziController().setDataObjectValue(intDataType, 100);
            }
        });
    }

    private void copyAssetsToDst(Context context, String srcPath, String dstPath) {

        try {
            InputStream is = context.getAssets().open(srcPath);
            FileOutputStream fos = new FileOutputStream(new File(dstPath));
            byte[] buffer = new byte[1024];

            int byteCount;
            while ((byteCount = is.read(buffer)) != -1) {
                fos.write(buffer, 0, byteCount);
            }

            fos.flush();
            is.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mDataFeeder == null) {
            return;
        }

        SharedData.get().removeManager();
        mDataFeeder.delete();
    }
}

显示效果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后知晚觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值