2016年4月

NDK-JNI开发笔记


2016/04/13

hello-jni项目(基于android studio 2.0)

(as2.2+ 可以用cmake 其实现,非常简单 不用.h文件 一开始的配置见 http://tools.android.com/tech-docs/external-c-builds 后面只要写个native方法 c文件中写对应的函数即可 )

  • 配置NDK 如果没下载NDK的话

    File->Settings Appearance & Behavior->System Settings ->Android SDK 右侧选择SDK Tools 勾选NDK更新 勾上更新重启项目。

local.properties里面如果没自动配置的话就这样配置(按上述步骤安装ndk 其目录就在sdk下)

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Wed Apr 13 00:40:11 CST 2016
ndk.dir=E\:\\BaiduYunDownload\\adt-bundle-windows-x86_64-20140702\\sdk\\ndk-bundle
sdk.dir=E\:\\BaiduYunDownload\\adt-bundle-windows-x86_64-20140702\\sdk
  • 新建Project,一个Activity(xml中带一个TextView)
  • 新建一个NdkJniUtils类 声明原生方法getCString();

    public class NdkJniUtil { public native String getCString(); }

  • 生成C/C++ 头文件

法1: IDE:Build->MakeProject 得到class 编译之后的class在目录下


SwipeRefreshLayout 载入时自动刷新


swipeRefreshLayout = (SwipeRefreshLayout) mView.findViewById(R.id.refresh_layout);
onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadPage = 0;
                mAdapter.cleanData();
                mAdapter.notifyDataSetChanged();
                Log.v("LishSearch", "开始加载数据");
            }
        };
        swipeRefreshLayout.setOnRefreshListener(onRefreshListener);
//显示加载
        swipeRefreshLayout.post(new Runnable(){
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }});
        onRefreshListener.onRefresh();


手动为Linux设置Swap(虚拟内存)


起步

买的服务器初始状态未配置swap,如果升配从内存从1G到2G,价格翻了一倍,那就走其他路径来“扩大”内存吧。

开始

1、创建用于交换分区的文件

dd if=/dev/zero of=/mnt/swap bs=block_size count=number_of_block

block_size、number_of_block 大小可以自定义,如:bs=1M count=1024,那么久会创建1024个bs=1M的区块(文件大小1024M)


自定义ProgressDialog


起步

主要实现方法为使用Dialog,将布局改写为ProgressDialog

编码

先编写主要界面布局my_lodding_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:wheel="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dialog_view"
    >
    <com.pnikosis.materialishprogress.ProgressWheel
        android:id="@+id/progress_wheel"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerHorizontal="true"
        wheel:matProg_barColor="@color/mainColor"
        wheel:matProg_progressIndeterminate="true"
        />
</LinearLayout>

其中com.pnikosis.materialishprogress.ProgressWheel 为github开源组件,下载地址