編輯:關於Android編程
JNI的全稱是Java Native Interface,它提供了若干的API實現了Java和其他語言的通信(主要是C和C++)。
1、代碼的保護。由於apk的java層代碼很容易被反編譯,而C/C++庫反匯難度較大。
2、可以方便地使用現存的開源庫。大部分現存的開源庫都是用C/C++代碼編寫的。
3、提高程序的執行效率。將要求高性能的應用邏輯使用C開發,從而提高應用程序的執行效率。
4、便於移植。用C/C++寫得庫可以方便在其他的嵌入式平台上再次使用。
為什麼使用JNI?
JNI的目的是使java方法能夠調用c實現的一些函數。
所以從這裡可以看出,是先有NDK開發,然後才有了JNI的調用。
配置環境變量
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include#include /* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java */ //jstring //Java_com_bazhangkeji_MainActivity_stringFromJNI( JNIEnv* env, // jobject thiz ) //{ // return (*env)->NewStringUTF(env, "Hello from JNI !"); //} JNIEXPORT jstring JNICALL Java_com_xzh_ndkdemo_MainActivity_stringFromJNI(JNIEnv *env, jobject instance) { // return (*env)->NewStringUTF(env, returnValue); return (*env)->NewStringUTF(env, "Hello from JNI !"); }
# Copyright (C) 2009 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello_jni LOCAL_SRC_FILES := hello_jni.c include $(BUILD_SHARED_LIBRARY)
LOCAL_MODULE := hello_jni LOCAL_SRC_FILES := hello_jni.c必須和.c文件對應,然後我們在build.gradle中加入如下語句:
externalNativeBuild { ndkBuild { path file("src\\main\\jni\\Hello.mk") } }然後rebuild 。 測試Activity
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { System.out.println("hello ndkdemo"+stringFromJNI()); } public native String stringFromJNI(); static { System.loadLibrary("hello_jni"); } }
Android手勢事件的沖突跟點擊事件的分發過程息息相關,由三個重要的方法來共同完成,分別是:dispatchTouchEvent、onInterceptTouchEve
隨著Android應用增多,功能越來越復雜,布局也越來越豐富了,而這些也成為了阻礙一個應用流暢運行,因此,對復雜的功能進行性能優化是創造高質量應用的基礎,本
本文章的導航欄代碼參考了viewpagerindicator的實現。本文敘述的是之前版本的qq或微信中,底部的圖標加文字的導航欄的實現。 本例子依賴viewpagerin
第一個動畫文件btn_anim.xml 2-在res文件夾 anim文件夾下面,建立第二個文件layout_anim.xml):